gpt4 book ai didi

android - 从 sqlite 数据库中删除所有表

转载 作者:IT老高 更新时间:2023-10-28 23:31:12 57 4
gpt4 key购买 nike

我做了很多研究,但找不到合适的方法来删除 SQLite 数据库中的所有表。最后,我编写了一个代码来从数据库中获取所有表名,并且我尝试使用检索到的表名一个一个地删除这些表。它也没有奏效。

请建议我一种从数据库中删除所有表的方法。

这是我使用的代码:

public void deleteall(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
do
{
db.delete(c.getString(0),null,null);
}while (c.moveToNext());
}

function deleteall() 在按钮点击时被调用,代码如下:

public void ButtonClick(View view)
{
String Button_text;
Button_text = ((Button) view).getText().toString();

if(Button_text.equals("Delete Database"))
{
DatabaseHelper a = new DatabaseHelper(this);
a.deleteall();
Toast.makeText(getApplicationContext(), "Database Deleted Succesfully!", Toast.LENGTH_SHORT).show();
}}

最佳答案

使用DROP TABLE:

// query to obtain the names of all tables in your database
Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
List<String> tables = new ArrayList<>();

// iterate over the result set, adding every table name to a list
while (c.moveToNext()) {
tables.add(c.getString(0));
}

// call DROP TABLE on every table name
for (String table : tables) {
String dropQuery = "DROP TABLE IF EXISTS " + table;
db.execSQL(dropQuery);
}

关于android - 从 sqlite 数据库中删除所有表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38672579/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com