我将数据插入到 sqlite 数据库表中,当我插入第一条记录时,它插入成功,但第二条和所有下一条记录都没有插入。这是我的数据库助手类。我在 Firefox 的 sqlite manager 中尝试了相同的查询,它们工作正常。我不知道为什么他们不在我的代码中工作。
private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + "( " + KEY_ROWID +
" INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DESCRIPTION + " text, "+ KEY_WHY+" text , "+ KEY_ACCOUNTABILITY+
" text , "+KEY_ALARM_TYPE+" text , "+KEY_ALARM_VOLUME+" text, "+KEY_BG_IMAGE+" text, "+KEY_CATEGORY+" text, "+KEY_DATE_TIME+ " datetime "+" , "+KEY_STATUS+" text );";
public void addRecord(String description,String why, String accontability, String alarm_type,
String alarm_vol, String bg_image, String category, Date goalDateTime,String status) {
String query = "INSERT INTO " + DATABASE_TABLE + " VALUES(null,'"+ description + "', '" + why + "', '" + accontability +"','" + alarm_type +
"', '" + alarm_vol + "', '" + bg_image +"','" +category+ "','" + goalDateTime +"','"+status+"');";
Log.v("--->", query);
try{
mDB.execSQL(query);
} catch (Exception ex) {
Log.v("database error", ex.toString());
}
}
在这里我在表中添加数据
try {
/*
* dbHelper = new GoalDbAdapter(this); dbHelper.open();
* Log.v("database ","database opened"); // dbHelper. fillData();
*/
db = new GoalDbAdapter(this);
db.open();
String imagePath = pref.getValue("imagePath", "");
String goalCateg = pref.getValue("categText", "");
String vol = pref.getValue("vol", "");
String alarmType= pref.getValue("ringTonePath", "");
db.addRecord(goal_desc, goal_why, accountability, alarmType, vol, imagePath, goalCateg, goalDate,"");
cursor = db.getTop3Goals();
Log.v("cursor count in add act",".."+cursor.getCount());
cursor.close();
db.close();
} catch (Exception ex) {
Log.v("database error", ex.toString());
}
这是我获取目标的代码,我无法获取所有数据,它只返回第一个目标而不返回其他目标。所有这些方法都只返回第一个目标。
public Cursor getGoals() {
Cursor c_all = mDB.rawQuery("SELECT * FROM " + DATABASE_TABLE, null);
int count= c_all.getCount();
Log.v("cursor count in db all goals", c_all.getCount()+"..");
c_all.moveToFirst();
return c_all;
}
//SELECT * FROM table ORDER BY date DESC LIMIT 5
public Cursor getTop3Goals() {
Cursor c = mDB.rawQuery("SELECT * FROM " + DATABASE_TABLE + " ORDER BY "+ KEY_DATE_TIME + " DESC LIMIT 3", null);
c.moveToFirst();
return c;
}
public Cursor getGoalsByDate() {
Cursor c = mDB.rawQuery("SELECT * FROM " + DATABASE_TABLE + " ORDER BY "+ KEY_DATE_TIME + " DESC ", null);
c.moveToFirst();
return c;
}