android update delete database table, delete all table rows from SQLite database table

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

The 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}

Here is the LogCat output I get for the following query:

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

Just do it:

db.delete(DATABASE_TABLE, null, null);

Note that using rawQuery should work, but there may be potential security risks.

edit:

Problems encountered when using

db.execSQL

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

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

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

Contact here is my name..try it..it works for me..

Use this method to delete all values ​​from the database table in the database.

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

This work is fine.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324116780&siteId=291194637
Recommended