我正在尝试使用删除我的数据的delete
功能,但它总是显示我很遗憾。SQLiteDataBase
这是我的方法:
private void del(){
String id = editId.getText().toString();
SQLiteDatabase db = dbhelper.getWritableDatabase();
db.delete(TABLE_NAME, _ID + "=" + id, null);
cleanEditText();
}
但是,如果我使用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