我試圖使用delete
函數SQLiteDataBase
來刪除我的數據,但它總是顯示我不幸的是。爲什麼我不能在Android上使用SQLiteDatabase的刪除功能
這是我的方法:
但是,如果我用db.execSQL("DELETE from friends");
,而不是delete
功能,它的工作原理,並刪除所有數據。
如何使delete
函數正常工作?
The LogCat say the following:
11-29 23:55:02.597: E/AndroidRuntime(29207): FATAL EXCEPTION: main
11-29 23:55:02.597: E/AndroidRuntime(29207): android.database.sqlite.SQLiteException: no such column: Adam: , while compiling: DELETE FROM friends WHERE _id=Adam
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:266)
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:88)
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1747)
11-29 23:55:02.597: E/AndroidRuntime(29207): at com.tonycube.demo.SQLiteDemoActivity.del(SQLiteDemoActivity.java:163)
11-29 23:55:02.597: E/AndroidRuntime(29207): at com.tonycube.demo.SQLiteDemoActivity.onClick(SQLiteDemoActivity.java:86)
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.view.View.performClick(View.java:3574)
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.view.View$PerformClick.run(View.java:14293)
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.os.Handler.handleCallback(Handler.java:605)
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.os.Handler.dispatchMessage(Handler.java:92)
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.os.Looper.loop(Looper.java:210)
11-29 23:55:02.597: E/AndroidRuntime(29207): at android.app.ActivityThread.main(ActivityThread.java:4441)
11-29 23:55:02.597: E/AndroidRuntime(29207): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 23:55:02.597: E/AndroidRuntime(29207): at java.lang.reflect.Method.invoke(Method.java:511)
11-29 23:55:02.597: E/AndroidRuntime(29207): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
11-29 23:55:02.597: E/AndroidRuntime(29207): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
11-29 23:55:02.597: E/AndroidRuntime(29207): at dalvik.system.NativeStart.main(Native Method)
這是助手:
public class DBHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "demo.db";
private final static int DATABASE_VERSION = 1;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
final String INIT_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " CHAR, " +
TEL + " CHAR, " +
EMAIL + " CHAR);";
db.execSQL(INIT_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(DROP_TABLE);
onCreate(db);
}
}
然後,這是我的點擊功能:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAdd:
add();
break;
case R.id.btnDel:
del();
break;
case R.id.btnUpdate:
update();
break;
default:
break;
}
show();
showInList();
}
結果將是這一點。 http://i.imgur.com/Y2TEf.png
是不是'DROP'而不是'DELETE'? – Bigflow
LogCat說什麼? – Ahmad
您確定,您在'TABLE_NAME'中有正確的內容嗎?另外,你應該使用'delete'('whereArgs')的第三個參數來提供用戶提供的參數(參見Prepared Statements)。 – dnet