Remove all table rows from SQLite database table

tharindlaksh picture tharindlaksh · Nov 15, 2011 · Viewed 55.7k times · Source

I want to remove all rows that i entered from my SQLite database table. The table name is tbltask. I tried to drop the table and delete * from table, but those are giving me runtime errors. I want to trigger this event in Button OnClickListner event.

Following code is what I tried:

String delete = "DELETE FROM "+DATABASE_TABLE;
db.rawQuery(delete, null);
db.delete(DATABASE_TABLE, null, null);

LogCat:

11-15 17:45:04.660: DEBUG/AndroidRuntime(300): Shutting down VM
11-15 17:45:04.660: WARN/dalvikvm(300): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): FATAL EXCEPTION: main 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): java.lang.NullPointerException 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): at database.com.android.DatabaseAccess.drop(DatabaseAccess.java:258) 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): at com.android.ExtraActivity$3$1.onClick(ExtraActivity.java:61) 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): at android.os.Looper.loop(Looper.java:123) 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): at android.app.ActivityThread.main(ActivityThread.java:4627) 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): at java.lang.reflect.Method.invokeNative(Native Method) 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): at java.lang.reflect.Method.invoke(Method.java:521) 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
11-15 17:45:04.710: ERROR/AndroidRuntime(300): at dalvik.system.NativeStart.main(Native Method) 
11-15 17:45:04.781: WARN/ActivityManager(58): Force finishing activity com.android/.ExtraActivity 
11-15 17:45:05.320: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{45061a70 com.android/.ExtraActivity} 
11-15 17:45:14.857: WARN/ActivityManager(58): Launch timeout has expired, giving up wake lock! 
11-15 17:45:15.402: WARN/ActivityManager(58): Activity idle timeout for HistoryRecord{450141d0 com.android/.WelcomActivity} 
11-15 17:45:20.572: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{45061a70 com.android/.ExtraActivity}

This is the LogCat output I get for following query:

        String delete = "DELETE FROM taskTable";
    db.execSQL(delete);

Answer

Matthieu picture Matthieu · Nov 15, 2011

Just do:

db.delete(DATABASE_TABLE, null, null);

Note that using rawQuery should work, but can be a potential security risk.

EDIT:

About the problem you have when you use

db.execSQL

Read the documentation, it says you should not use execSQL with INSERT, DELETE, UPDATE or SELECT