Delete function of my table

Asked

Viewed 174 times

1

I wonder why you’re giving this trouble.

I created a function to delete the table from my database and instated the button to perform this function, but when I go to test it presents me an error.

logcat error:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.allsport.miyonic.allsport, PID: 11125
                  android.database.sqlite.SQLiteException: near "*": syntax error (code 1): , while compiling: DELETE * FROM resultado
                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                      at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                      at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                      at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
                      at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
                      at Base.DbHelper.deletar(DbHelper.java:101)
                      at com.allsport.miyonic.allsport.ResultSimples.onOptionsItemSelected(ResultSimples.java:68)
                      at android.app.Activity.onMenuItemSelected(Activity.java:2908)
                      at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
                      at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)
                      at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
                      at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:663)
                      at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
                      at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                      at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
                      at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
                      at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
                      at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
                      at android.view.View.performClick(View.java:5198)
                      at android.view.View$PerformClick.run(View.java:21147)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Dbhelper :

 public void delete() {

        SQLiteDatabase db = getReadableDatabase();
        String sqlSelectTodosResult = "DELETE * FROM resultado";
        Cursor c = db.rawQuery(sqlSelectTodosResult, null);
    }

in my Activity:

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.resultado_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.deletar) {


            DbHelper dbhelp = new DbHelper(this);
            dbhelp.delete();

        }

        return super.onOptionsItemSelected(item);
    }

Thank you.

  • 1

    https://www.sqlite.org/lang_delete.html

1 answer

7


This is a syntax error, the *is the most.

The syntax of DELETE is:

DELETE FROM table_name
WHERE some_column=some_value; 

If you want to delete all records do not use the WHERE clause:

DELETE FROM table_name; 

Note:
The question refers in "delete the table", DELETE does not delete the table, just deletes records.

To delete the table you must use DROP TABLE:

DROP TABLE table_name;
  • 1

    It worked... thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.