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

时间:2015-01-20 09:36:32

标签: android android-sqlite

我是android中的新手。我尽力找到问题的解决方案,但对我来说似乎有点困难。

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

我得到以下提及错误。欢迎任何帮助或批评。提前谢谢。

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$5.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)

3 个答案:

答案 0 :(得分:3)

sqlite中没有TRUNCATE。 (即使有,你需要在关键字table和表名之间留一个空格。)

使用"DELETE FROM " + TABLE_NAME_MESSAGES代替删除表格中的所有行。

答案 1 :(得分:1)

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

答案 2 :(得分:-1)

完整的语法是:&#34; DELETE FROM&#34; + TABLE_NAME_MESSAGES +&#34 ;;&#34 ;;