22

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);
2
  • 1
    What errors do you get? Post the logcat output Commented Nov 15, 2011 at 4:52
  • 1
    what's the error you are getting.... just try this one without the rawquery part. sqLiteDatabase.delete(MYDATABASE_TABLE, null, null); Commented Nov 15, 2011 at 5:05

4 Answers 4

71

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

Sign up to request clarification or add additional context in comments.

Comments

0

Consensus is to drop and recreate the table, or you can use DELETE FROM tbltask which uses a performance operation similar to a TRUNCATE on other dbs.

Comments

0
 getWritableDatabase().execSQL("DELETE FROM " + "contacts" + ";");

Here contacs is my table name.. Try it..It works for me..

1 Comment

This explicitly contradicts the documentation: "Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE"
0

Delete all values from database table use this method in database.

private SQLiteDatabase odb;
public void deleteallvalues(){
     odb.delete(TABLE_NAME,null,null)
}

this work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.