android - 在Android中,如何从SQLite中删除所有项目

我想创建一个用户点击按钮的应用程序,清除SQLite数据库,以下是我到目前为止尝试过的内容 :


db.delete(TABLE_NAME, null, null);

我做错什么了?

时间:

以下是使用delete的方法的示例:


/**
 * Remove all users and groups from database.
 */
public void removeAll()
{
 // db.delete(String tableName, String whereClause, String[] whereArgs);
 // If whereClause is null, it will delete all rows.
 SQLiteDatabase db = helper.getWritableDatabase(); // helper is object extends SQLiteOpenHelper
 db.delete(DatabaseHelper.TAB_USERS, null, null);
 db.delete(DatabaseHelper.TAB_USERS_GROUP, null, null);
}

db.delete(TABLE_NAME.null.null);是删除表数据的正确语法,但是,我认为应该直接给出表名,而不用双引号括起来。这样试试


db.delete("TABLE_NAME", null, null);

可能有助于:

我称它为reset,因此,在生成表之后,只需执行db.reset (),然后在onCreate中添加默认值。


public void reset () throws SQLException {
 db = DBHelper.getWritableDatabase ();
 db.execSQL ("drop table"+TABLE_NAME);
 db.close ();
 this.DBHelper.onCreate (this.db);
}

使用此代码清除所有数据库内容


Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
String tableName="";
 if (c.moveToFirst()) {

 while ( !c.isAfterLast() ) {

 tableName = c.getString( c.getColumnIndex("name"));
 if(!tableName.equals('android_metadata')){
 db.execSQL("DROP TABLE '"+tableName+"'");
 }
 c.moveToNext();
 }
 }

c.close();


SQLiteDatabase db = this.getWritableDatabase(); //get database
 db.execSQL("DELETE FROM tablename"); //delete all rows in a table
db.close();

经典的mysql语句:


SQLiteDatabase sqLiteDatabase = context.openOrCreateDatabase(Database.DATABASE_NAME, Context.MODE_PRIVATE, null, null);
sqLiteDatabase.execSQL("DELETE FROM" + tableName1);
sqLiteDatabase.execSQL("DELETE FROM" + tableName2);
sqLiteDatabase.execSQL("DELETE FROM" + tableName3);
sqLiteDatabase.execSQL("DELETE FROM" + tableName4);

完美工作 : )

...