当用户点击按钮时,我试图清除数据库中的所有数据。我不确定如何在不删除整个数据库的情况下这样做。
(我希望列和表保持创建状态,以便他们以后仍然可以在应用中添加项目)
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();
}
});
}
}
我的记录数据库:
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;
}
}
我正在考虑在RecordsDatabase.java中添加clearInfo()
,但不知道我该怎么做。
答案 0 :(得分:1)
删除表格然后创建表格然后尝试从表格中删除数据会更快。
如果要进行整个删除,也可以删除整个数据库并重新创建。
答案 1 :(得分:1)
这样的事情可以起作用:
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();
}
});
当然,您应该根据自己的需要调整代码。