一段时间后无法从 Sqlite 数据库中删除行

Not able to delete Rows from Sqlitedatabase after some time intervel

我是 SQLite 的新手,在 android.I 中尽了最大的努力来解决问题,但对我来说似乎有点困难。

private LocalStorageHandler localstoragehandler;
localstoragehandler = new LocalStorageHandler(this);

    Handler handler = new Handler();

        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                localstoragehandler.deleteAll();
                localstoragehandler.close();
            }
        }, 20000);





 public void deleteAll() {
            db.execSQL("TRUNCATE table" + TABLE_NAME_MESSAGES);
            db.close();
        }

下面提一下error.any帮助或批评是welcome.Thank你提前

01-20 15:22:37.477: E/AndroidRuntime(3243): FATAL EXCEPTION: main
01-20 15:22:37.477: E/AndroidRuntime(3243): android.database.sqlite.SQLiteException: near "TRUNCATE": syntax error (code 1): , while compiling: TRUNCATE tableandroidim_messages
01-20 15:22:37.477: E/AndroidRuntime(3243):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at at.vcity.androidim.tools.LocalStorageHandler.deleteAll(LocalStorageHandler.java:92)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at at.vcity.androidim.Messaging.run(Messaging.java:253)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at android.os.Handler.handleCallback(Handler.java:615)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at android.os.Looper.loop(Looper.java:137)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at android.app.ActivityThread.main(ActivityThread.java:4812)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at java.lang.reflect.Method.invokeNative(Native Method)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at java.lang.reflect.Method.invoke(Method.java:511)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
01-20 15:22:37.477: E/AndroidRuntime(3243):     at dalvik.system.NativeStart.main(Native Method)

    01-20 14:58:35.148: E/AndroidRuntime(18003):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
    01-20 14:58:35.148: E/AndroidRuntime(18003):    at dalvik.system.NativeStart.main(Native Method)

看起来您在执行 SQL 语句删除数据之前使用 db.delete(DATABASE_NAME, null, null); 删除了整个数据库。您可能想要重新评估您要实现的目标 - 整个数据擦除,还是数据库本身的破坏?

sqlite 中没有TRUNCATE。 (即使有,您也需要在关键字 table 和 table 名称之间添加一个 space。)

改为使用 "DELETE FROM " + TABLE_NAME_MESSAGES 删除 table 中的所有行。

完整语法为:"DELETE FROM " + TABLE_NAME_MESSAGES+";";