我正在尝试在SQLite数据库(Android)中增加一个值,但是我得到了一个致命的错误:“没有这样的表格'在logcat和应用程序粉碎。但是该表存在于数据库中。我的代码可能有问题吗?任何帮助将不胜感激。这是我的更新代码和logcat输出。
a)更新代码
public void tupdateDaysLeft(){
if (daysLeftCur == null) {
daysLeftCur = findDaysLeft();
}
if (daysLeftCur.moveToFirst()) {
int columnIndex = daysLeftCursor.getColumnIndex(MsDbAdapter.COL_DAYSLEFT);
int daysToDel = daysLeftCur.getInt(columnIndex);
// int stt = daysToDel;
// daysle = stt;
mDb.execSQL("UPDATE "+TABLE_PRE+
" SET "+COL_DAYSLEFT+" = "+COL_DAYSLEFT+" - 1"+
" WHERE "+COL_ID+" = ?",
new Object[] { columnIndex });
}
}
b) - i Logcat输出1
07-16 23:43:36.938: E/SQLiteLog(20992): (1) no such table: TABLE_PRE
07-16 23:43:36.948: E/AndroidRuntime(20992): FATAL EXCEPTION: main
07-16 23:43:36.948: E/AndroidRuntime(20992): android.database.sqlite.SQLiteException: no such table: TABLE_PRE (code 1): , while compiling: update
TABLE_PRE set COL_DAYSLEFT = COL_DAYSLEFT + 1 where COL_ID = ?
07-16 23:43:36.948: E/AndroidRuntime(20992): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-16 23:43:36.948: E/AndroidRuntime(20992): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1013)
07-16 23:43:36.948: E/AndroidRuntime(20992): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:624)
07-16 23:43:36.948: E/AndroidRuntime(20992): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-16 23:43:36.948: E/AndroidRuntime(20992): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
b) - ii Logcat输出2
01-01 06:31:37.701: E/AndroidRuntime(6357): FATAL EXCEPTION: main
01-01 06:31:37.701: E/AndroidRuntime(6357): java.lang.NullPointerException
01-01 06:31:37.701: E/AndroidRuntime(6357): at com.tembk.mspre.database.MsDbAdapter.tupdateDaysLeft(MsDbAdapter.java:292)
01-01 06:31:37.701: E/AndroidRuntime(6357): at com.tembk.mspre.pre.PreStatus$3.onClick(PreStatus.java:79)
c)表格定义
private static final String TABLE_PRE = "pre";
修改
d)表常量/字段
private static final String TABLE_PRE = "pre";
public static final String COL_ID = "_id";
public static final String COL_BEGINDATE = "begindate";
public static final String COL_TERMEND = "termend";
public static final String COL_DAYSLEFT = "daysleft";
public static final String COL_DATE_TIME = "pre_date_time";
e)表格创建SQL
private static final String PRE_TABLE_CREATE =
"create table " + TABLE_PRE + " ("
+ COL_ID + " integer primary key autoincrement, "
+ COL_BEGINDATE + " text not null, "
+ COL_TERMEND + " text not null, "
+ COL_DAYSLEFT + " integer not null, "
+ COL_DATE_TIME + " text not null " + ")";
答案 0 :(得分:2)
数据库不知道Java代码中有名为TABLE_PRE
或daysle
的符号;你必须用Java替换它们。
此外,COL_ID
可能是ID列的名称,但您需要与所需记录中的值进行比较。
mDb.execSQL("UPDATE "+TABLE_PRE+
" SET "+COL_DAYSLEFT+" = ?"+
" WHERE "+COL_ID+" = ?",
new Object[] { daysle + 1, idOfTheRowToUpdate });
但是,要增加该值,您不需要读取代码中的旧值;你可以直接在SQL中添加:
mDb.execSQL("UPDATE "+TABLE_PRE+
" SET "+COL_DAYSLEFT+" = "+COL_DAYSLEFT+" + 1"+
" WHERE "+COL_ID+" = ?",
new Object[] { idOfTheRowToUpdate });
答案 1 :(得分:0)
我通过使用内容值来解决此问题。我在数据库适配器中创建了一个方法,并在我的主要活动中调用它,传递了我需要的值。它看起来很粗糙,但工作正常。
1)数据库适配器更新方法
public boolean updatePreDL(Object mRowId, int daysleft, String preDateTime) {
ContentValues newValues = new ContentValues();
newValues.put(COL_DAYSLEFT, daysleft);
newValues.put(COL_DATE_TIME, preDateTime);
return mDb.update(TABLE_PRE, newValues, COL_ID + "=" + mRowId, null) > 0;
}
2)按钮单击调用(上面的数据库适配器更新方法)
updateDays.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
mDbHelper.updatePreDL(MsDbAdapter.COL_ID,
daysLeftCur.getInt(daysLeftCur.getColumnIndex(MsDbAdapter.COL_DAYSLEFT))-1,
MsDbAdapter.COL_DATE_TIME);
}
});
答案 2 :(得分:-1)
我认为你应该使用它:
mDb.execSQL("update TABLE_PRE set COL_DAYSLEFT = "+ (daysle + 1) +" where COL_ID = ?", new Object[] {COL_ID});
}
而不是:
mDb.execSQL("update TABLE_PRE set COL_DAYSLEFT = daysle + 1 where COL_ID = ?", new Object[] {COL_ID});