为什么我不能在Android上使用SQLiteDatabase的删除功能

时间:2012-11-29 15:14:25

标签: android-sqlite

我正在尝试使用delete的{​​{1}}功能删除我的数据,但它始终显示不幸

这是我的方法:

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);
    }

}

结果将是这样。 http://i.imgur.com/Y2TEf.png

2 个答案:

答案 0 :(得分:1)

“我正在尝试使用SQLiteDataBase的删除功能来删除我的数据,但它总是显示给我” - 总是告诉你什么?运行代码时会出现什么错误(如果有的话)?

以下是使用delete的示例:

db.delete(TABLE_NAME, _ID + " = ?", new String[] { Integer.toString(id) });

您应始终使用参数化查询来帮助缓解SQL注入攻击。

你说db.execSQL("DELETE from friends");有效,但这是一个完全不同的查询,不按ID选择 - 也许你的_ID常量是错误的。

答案 1 :(得分:0)

替换

db.delete(TABLE_NAME, _ID + "=" + id, null);

使用以下行

db.delete(TABLE_NAME, _ID + "=" + "'"+id+"'", null);

让我知道结果