-2

我正在尝试使用删除我的数据的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

4

2 回答 2

1

“我正在尝试使用 SQLiteDataBase 的删除功能来删除我的数据,但它总是告诉我”——总是告诉你什么?运行代码时遇到什么错误(如果有)?

下面是一个使用删除的例子:

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

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

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

于 2012-11-29T15:36:03.033 回答
0

代替

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

使用以下行

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

让我知道结果

于 2012-11-29T15:27:29.907 回答