Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在单个命令中使用sqllite截断数据库(清除数据库中所有表中的记录)_Android_Sql_Database_Sqlite - Fatal编程技术网

Android 如何在单个命令中使用sqllite截断数据库(清除数据库中所有表中的记录)

Android 如何在单个命令中使用sqllite截断数据库(清除数据库中所有表中的记录),android,sql,database,sqlite,Android,Sql,Database,Sqlite,我正在从数据库的所有表中搜索批量删除 DELETE FROM TABLE1,TABLE2,TABLE3; --(Is it possible) 就像我们有批量插入 INSERT INTO TABLE (a,b,c) VALUES (1,2,3),(4,5,6),(7,8,9); 我的代码,我不满意,因为它是这样的。它能够做到这一点 Delete from table1; Delete from table2; Delete from table3; public void truncat

我正在从数据库的所有表中搜索批量删除

DELETE FROM TABLE1,TABLE2,TABLE3; --(Is it possible)
就像我们有批量插入

INSERT INTO TABLE (a,b,c) VALUES (1,2,3),(4,5,6),(7,8,9);
我的代码,我不满意,因为它是这样的。它能够做到这一点

Delete from table1;
Delete from table2;
Delete from table3;

public void truncateDatabase(){
ArrayList tables=新的ArrayList();
Cursor Cursor=Database.getInstance().getWritableDatabase().rawQuery(“从sqlite_master中选择名称,其中type='table',null);
if(cursor.moveToFirst()){
而(!cursor.isAfterLast()){
tables.add(cursor.getString(0));
cursor.moveToNext();
}
}
cursor.close();
for(字符串表:表){
Database.getInstance().getWritableDatabase().execSQL(“从”+表中删除”);
Database.getInstance().getWritableDatabase().execSQL(“真空”);
}
}
如中所示,单个DELETE语句可用于单个表

执行多个DELETE语句没有错

不过,您应该只执行一次真空,并考虑使用事务:

SQLiteDatabase db=Database.getInstance().getWritableDatabase();
db.beginTransaction();
试一试{
for(字符串表:表){
db.execSQL(“从”+表中删除);
}
db.setTransactionSuccessful();
}最后{
db.endTransaction();
}
db.execSQL(“真空”);

我正在使用delete from table是否有可能使用任何命令对数据库中的所有表进行trunacate感谢CL的帮助,但是如何优化我的代码,因为at目前正在循环delete和vaccum。根据你的建议,(但你应该只执行一次真空,并考虑使用一个交易。)任何一个例子,我将非常感谢你。
public void truncateDatabase() {
    ArrayList<String> tables = new ArrayList<>();
    Cursor cursor = Database.getInstance().getWritableDatabase().rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
    if (cursor.moveToFirst()) {
        while (!cursor.isAfterLast()) {
            tables.add(cursor.getString(0));
            cursor.moveToNext();
        }
    }
    cursor.close();

    for (String table : tables) {
        Database.getInstance().getWritableDatabase().execSQL("DELETE FROM " + table);
        Database.getInstance().getWritableDatabase().execSQL("VACUUM");
    }
}