2

CREATE TABLE

private void createSerialDateTable() {
    mDatabase.execSQL(
            "CREATE TABLE IF NOT EXISTS SerialDate (\n" +
                    "    id INTEGER NOT NULL CONSTRAINT SerialDate_pk PRIMARY KEY AUTOINCREMENT,\n" +
                    "    name varchar(200) NOT NULL,\n" +
                    "    joiningdate datetime NOT NULL\n" +
                    ");"
    );
}

INSERT TABLE

 String insertSQL = "INSERT INTO SerialDate \n" +
                "(name, joiningdate)\n" +
                "VALUES \n" +
                "(?, ? );";
        mDatabase.execSQL(insertSQL, new String[]{name, joiningDate});

Delete all items from List I try it But it not working.

clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

          //  serialDateList.clear();
            Cursor cursorSerialDate = mDatabase.rawQuery("DELETE  FROM SerialDate", null);
            cursorSerialDate.close();

            adapter.notifyDataSetChanged();
        }
    });

How can I delete all data definitely.
Please. Help me !

SonhnLab
  • 321
  • 1
  • 11
Ha Jer
  • 23
  • 1
  • 6
  • Possible duplicate of [How to delete all record from table in sqlite with Android?](https://stackoverflow.com/questions/9599741/how-to-delete-all-record-from-table-in-sqlite-with-android) –  Jul 10 '18 at 16:22
  • Also another solution but it may be heavier drop the table and recreate another one – Basil Battikhi Jul 10 '18 at 16:23

4 Answers4

1

Try using this code to delete all rows in a table :

database.delete(tablename, null,null);
Belbahar Raouf
  • 760
  • 1
  • 4
  • 17
1

The better way is to create one common method in DataBaseHelper class

public void deleteTableF(String tablename) {

               String selectQuery = "DELETE FROM " + tablename;

               SQLiteDatabase db= this.getWritableDatabase();

               db.execSQL(selectQuery);
            }

And call deleteTableF method where you want to delete a particular table in your code.

SQLiteDatabase db =  = new DataBaseHelper(this); // Dbhelper class object
 db.deleteTableF("table_name");
vinod
  • 1,126
  • 13
  • 18
0

rawQuery is used for select statements to fetch rows. Use execSql instead or the delete method

0

You could try this one

clear.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        db.execSQL("DELETE FROM "+ TABLE_NAME);
        db.close();

        // Other codes here
    }
});
  • SerialDate Expression Expected !! what should I do plz !! [link]( https://imgur.com/hf3QrUs) – Ha Jer Jul 11 '18 at 11:17