从SQLite中的表中删除行时出现问题



我有一个这样的数据库:

public static final String TABLE = "NOTE_TABLE";
public static final String ID = "COLUMN_ID";
public static final String CHARS = "COLUMN_CHARS";
public static final String DATE = "COLUMN_DATE";
public void onCreate(SQLiteDatabase db) {
String createTableStatemnet = "CREATE TABLE " + TABLE +
" (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + CHARS + " TEXT," + DATE + " TEXT)";
db.execSQL(createTableStatemnet);
}

我定义了一种删除行的方法:

public void removeOne(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE, "COLUMN_CHARS=?", new String[]{Integer.toString(id)});
db.close();
}

并从onClickListener()调用它

public void remove(View view) {
dataBase = new DataBase(MainActivity.this);
// 1 here is the id of the row i want to delete
dataBase.removeOne(1);
}

但当我检查数据库检查器时,没有删除任何行。我做错了什么?

尝试

public void removeOne(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM TABLE WHERE COLUMN_ID=?", new Int[] { id });
db.close();
}

public void removeOne(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE, "COLUMN_ID=?", new Int[] { id });
db.close();
}

因为您正在传递id(Int(,并且正试图使用COLUMN_CHARS(String(进行删除

希望它能起的作用

最新更新