How To Delete All Table Records In Sqlite?
I get JSON value from a server and save the data into a database in Sqlite in Android, I create a database and a table with SQLiteOpenHelper class: public class ActivityTableDBOpen
Solution 1:
You should first initialize you SQLiteDatabase, so convert this:
SQLiteDatabase database;
database.delete(ActivityTableDBOpenHelper.ACTIVITY_TABLE,null,null);
to this:
SQLiteDatabasedatabase=newSQLiteDatabase(this); // or dbHelper.getWritableDatabase(); if you have a dbHelper
database.delete(ActivityTableDBOpenHelper.ACTIVITY_TABLE,null,null);
Else the database object is null and you get the error. Here you could find a whole example for all operations http://www.vogella.com/tutorials/AndroidSQLite/article.html
Solution 2:
db.execSQL("delete from "+ TABLE_NAME);
Solution 3:
The problem with your call
SQLiteDatabase database;
database.delete(ActivityTableDBOpenHelper.ACTIVITY_TABLE,null,null);
is that your database-object is null. Call SQLiteDatabase db = getWritableDatabase() and proceed.
To delete information from your database or specific tables you can also use basic SQL-Queries: http://www.w3schools.com/sql/sql_delete.asp
db.rawQuery("DELETE FROM " + table + " WHERE " + args);
Solution 4:
I found where I was wrong :D
thanks to @Gabriella Angelova
I have to initializing SQLiteDatabase and SQLiteOpenHelper
publicclassActivityTableActivityextendsActionBarActivity {
SQLiteDatabase database;
ActivityTableDBOpenHelper dbOpenHelper;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_table);
dbOpenHelper = newActivityTableDBOpenHelper(this);
database = dbOpenHelper.getWritableDatabase();
btnDeleteATData = (Button) findViewById(R.id.btnDeleteATData);
dataSource = newActivityTableDataSource(this);
dataSource.open();
btnDeleteATData.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
database.delete(ActivityTableDBOpenHelper.ACTIVITY_TABLE, null, null);
}
});
}
Solution 5:
//ourDb ->SqliteDatabase object//TABLE_NAME -> publicvoiddeleteDatafromAllTables() {
ourDb.execSQL("delete from " + TABLE_NAME);
}
Post a Comment for "How To Delete All Table Records In Sqlite?"