简体   繁体   中英

Clear data from sqlite db

I'm trying to clear all data from the database when a user clicks on a button. I'm not sure how to do it without deleting the entire database.

(I want the columns and tables to remain created, so they can still add items to it later in the app)

public class MyProgress extends BaseActivity{
    Button btnClear;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_progress);
        mToolBar = activateToolbar();
        setUpNavigationDrawer();
        getSupportActionBar().setTitle("My Progress");
        btnClear.findViewById(R.id.btnClear);

        RecordsDatabase helper = new RecordsDatabase(this);
        Cursor c = helper.getRecords();
        ListView listViewRec = (ListView) findViewById(R.id.listViewRecord);
        final TodoCursorAdapter adapter = new TodoCursorAdapter(this, c);
        listViewRec.setAdapter(adapter);


        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //NEED TO CLEAR DB HERE
                adapter.notifyDataSetChanged();
            }
        });
    }
}

My recordsDatabase:

public class RecordsDatabase extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;

    public RecordsDatabase(Context context) {
        super(context, RecordContract.RecordInfo.DATABASE_NAME, null, DATABASE_VERSION);
        Log.d("Database operations", "Database created");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + RecordContract.RecordInfo.TABLE_NAME + " ("
                + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                        + RecordContract.RecordInfo.RECORDS_DATE + " TEXT NOT NULL,"
                + RecordContract.RecordInfo.RECORDS_SQUAT + " TEXT NOT NULL,"
                + RecordContract.RecordInfo.RECORDS_BENCH + " TEXT NOT NULL,"
                + RecordContract.RecordInfo.RECORDS_DEAD + " TEXT NOT NULL,"
                + RecordContract.RecordInfo.RECORDS_TOTAL + " TEXT NOT NULL)"

        );
        Log.d("Database operations", "Table created");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void putInformation(String date, String squat, String bench, String dead, String total) {
        SQLiteDatabase SQ = getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put(RecordContract.RecordInfo.RECORDS_DATE, date);
        contentValues.put(RecordContract.RecordInfo.RECORDS_SQUAT, squat);
        contentValues.put(RecordContract.RecordInfo.RECORDS_BENCH, bench);
        contentValues.put(RecordContract.RecordInfo.RECORDS_DEAD, dead);
        contentValues.put(RecordContract.RecordInfo.RECORDS_TOTAL, total);
        long k = SQ.insert(RecordContract.RecordInfo.TABLE_NAME, null, contentValues);
        Log.d("Database operations", "Record added(1 row)");

    }

    public Cursor getRecords() {
        SQLiteDatabase SQ = getReadableDatabase();
        String[] columns = {RecordContract.RecordInfo._ID, RecordContract.RecordInfo.RECORDS_DATE, RecordContract.RecordInfo.RECORDS_SQUAT, RecordContract.RecordInfo.RECORDS_BENCH
        , RecordContract.RecordInfo.RECORDS_DEAD, RecordContract.RecordInfo.RECORDS_TOTAL};
        Cursor CR = SQ.query(RecordContract.RecordInfo.TABLE_NAME, columns,null, null, null, null, null);
        return CR;

    }
}

I was thinking of adding a clearInfo() in the RecordsDatabase.java, but not sure how I would do it.

It will be faster to drop table and then create table then trying to delete the data from the table.

You can also delete the entire database and recreate it if you want to do a entire delete.

Something like this sould work:

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

    sqLiteDatabase = dbHelper.getWritableDatabase();
    sqLiteDatabase.delete(RecordContract.RecordInfo.TABLE_NAME, null, null);
    sqLiteDatabase.execSQL("delete from " + RecordContract.RecordInfo.TABLE_NAME);
    sqLiteDatabase.close();

    adapter.notifyDataSetChanged();
    }
});

Of course, you should adapt code for your purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM