我正在尝试创建一个需要在具有3列的SQL数据库中存储数据的应用程序。问题是数据没有添加到数据库中。我正在使用 getCount()查看数据是否已添加,但始终返回0.
我的 DataHandler.class 如下
public final class DataHandler
{
FeedReaderDbHelper mDbHelper;
SQLiteDatabase db;
Context ctx;
public DataHandler(Context ctx)
{
this.ctx=ctx;
mDbHelper=new FeedReaderDbHelper(ctx);
}
public static final String TABLE_NAME = "History";
public static final String COLUMN_NAME_NAME1 = "na1";
public static final String COLUMN_NAME_NAME2 = "na2";
public static final String COLUMN_NAME_RESULT = "result";
private static final String CREATE_TABLE= "CREATE TABLE "+ TABLE_NAME + " ("+COLUMN_NAME_NAME1+ " TEXT NOT NULL,"+ COLUMN_NAME_NAME2 + " TEXT NOT NULL," + COLUMN_NAME_RESULT + " TEXT NOT NULL);";
private static final String SQL_DELETE_ENTRIES ="DROP TABLE IF EXISTS " + TABLE_NAME;
public class FeedReaderDbHelper extends SQLiteOpenHelper
{
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";
public FeedReaderDbHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
}
public DataHandler open()
{
db=mDbHelper.getWritableDatabase();
return this;
}
public void close()
{
mDbHelper.close();
}
public void Insert(String n1,String n2,int r)
{
ContentValues values = new ContentValues();
values.put(COLUMN_NAME_NAME1,n1);
values.put(COLUMN_NAME_NAME2, n2);
switch(r)
{
case 1:values.put(COLUMN_NAME_RESULT,"a");
break;
case 2:values.put(COLUMN_NAME_RESULT,"b");
break;
case 3:values.put(COLUMN_NAME_RESULT,"c");
break;
case 4:values.put(COLUMN_NAME_RESULT,"d");
break;
case 5:values.put(COLUMN_NAME_RESULT,"e");
break;
case 6:values.put(COLUMN_NAME_RESULT,"f");
break;
}
}
public Cursor returnData()
{
return db.query(TABLE_NAME, new String[]{COLUMN_NAME_NAME1, COLUMN_NAME_NAME2,COLUMN_NAME_RESULT}, null, null, null, null, null);
}
}
我的数据插入代码是 -
handler=new DataHandler(getBaseContext());
handler.open();
handler.Insert(name1, name2, ans);
handler.close();
数据查询代码是 -
public class HistoryPage extends ActionBarActivity
{
DataHandler handler;
int i,num;
String nm1,nm2,result;
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
mainListView = (ListView) findViewById( R.id.mainListView );
handler=new DataHandler(getBaseContext());
handler.open();
Cursor c=handler.returnData();
c.moveToFirst();
num=c.getCount();
Toast.makeText(getApplicationContext(), ""+num, Toast.LENGTH_LONG).show();
String[] Results = new String[4];
for(i=0 ; i<num ; i++)
{
nm1=c.getString(0).toString();
nm2=c.getString(1).toString();
result=c.getString(2).toString();
Results[i]=nm1+" & "+nm2+" - "+result;
c.moveToNext();
}
Toast.makeText(getApplicationContext(), Results[0], Toast.LENGTH_LONG).show();
handler.close();
ArrayList<String> hstry = new ArrayList<String>();
hstry.addAll(Arrays.asList(Results));
listAdapter = new ArrayAdapter<String>(this, R.layout.textv,hstry);
mainListView.setAdapter( listAdapter );
}
}
答案 0 :(得分:3)
指定列的正确方法是:
private static final String KEY_USERNAME = "username";
public static final int COL_USERNAME = 1;
此外,您的表创建字符串也不正确。正确的方法是这样的:
public static final String USER_TABLE_CREATE = "CREATE TABLE " + USER_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_USERNAME + " VARCHAR UNIQUE, " +
KEY_PASSWORD + " VARCHAR NOT NULL, " +
KEY_EMAIL + " VARCHAR NOT NULL, " +
KEY_FIRSTNAME + " VARCHAR NOT NULL, " +
KEY_LASTNAME + " VARCHAR NOT NULL, " +
"creation " + " DATETIME, " +
"lastlogin " + " DATETIME);";
然后你将构建表:
db.execSQL(USER_TABLE_CREATE);
让我们先从重写数据库处理程序开始,然后从那里开始。
此外,您的插入数据处理程序在功能上受到挑战。不知道在哪里插入数据。例如,这是一个产品插入处理程序:
public void addNewProduct(String cPRODUCTNAME, String cPRODUCTDESCRIPTION,
String cPRODUCTCODE, String cPRODUCTCOST, String cPRODUCTPRICE,
String cPRODUCTSTOCK, String cPRODUCTREORDER, String cTAXABLE,
String cPRODUCTNOTES) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cvProduct = new ContentValues();
cvProduct.put(KEY_PRODNAME, cPRODUCTNAME);
cvProduct.put(KEY_PRODDESCRIP, cPRODUCTDESCRIPTION);
cvProduct.put(KEY_PRODCODE, cPRODUCTCODE);
cvProduct.put(KEY_PRODCOST, cPRODUCTCOST);
cvProduct.put(KEY_PRODPRICE, cPRODUCTPRICE);
cvProduct.put(KEY_PRODSTOCK, cPRODUCTSTOCK);
cvProduct.put(KEY_PRODREORDER, cPRODUCTREORDER);
cvProduct.put(KEY_TAXABLE, cTAXABLE);
cvProduct.put(KEY_PRODNOTES, cPRODUCTNOTES);
db.insert(PRODUCTS_TABLE, null, cvProduct);
db.close();
}
第2行到最后一行说明了要将数据插入的表,null,然后是要插入的值。在这种情况下,所有上面列出的内容值。
如果要更新而不是插入,则只需添加WHERE claus和cahnge插入即可更新。
答案 1 :(得分:2)
我想你忘了把数据插入数据库。 你应该将这一行放在insert()函数中:
db.insert(TABLE_NAME, null, values);