android - 如果 sqflite 颤振中存在,则删除表并重新创建
问题描述
如果存在,我想在填充服务器数据之前删除表。
我正在尝试的查询
deleteOptionTable() async {
final db = await database;
db.rawDelete("Delete * from option");
}
dropTable() async {
final db = await database;
db.query('SELECT * FROM cloudnet360.db WHERE name =option and type=table');
}
我已经尝试过这样的事情,但什么也没发生。
void _insertOption(OptionsGroupList option) async {
int idd = dbHelper.dropTable();
print('DROP TABLE: $idd');
PreferencesConnector myprefs= PreferencesConnector();
String merchantid=await myprefs.readString('merchantid');
String hashkey=await myprefs.readString('hashkey');
Map<String, dynamic> row = {
DatabaseHelper.columnGroupId:option.grouprowid ,
DatabaseHelper.columnGroupName: option.groupname,
DatabaseHelper.columnIsRequired:option.isrequired ,
DatabaseHelper.columnMerchantId: merchantid,
DatabaseHelper.columnMerchantHashKey: hashkey,
};
int id = await dbHelper.insertOption(row);
print('inserted option row id: $id');
}
解决方案
正如问题中所问的那样,我们需要一种方法来删除表(如果存在)并在颤振中重新创建它并使用 sqflite 包......
Future<void> DropTableIfExistsThenReCreate() async {
//here we get the Database object by calling the openDatabase method
//which receives the path and onCreate function and all the good stuff
Database db = await openDatabase(path,onCreate: ...);
//here we execute a query to drop the table if exists which is called "tableName"
//and could be given as method's input parameter too
await db.execute("DROP TABLE IF EXISTS tableName");
//and finally here we recreate our beloved "tableName" again which needs
//some columns initialization
await db.execute("CREATE TABLE tableName (id INTEGER, name TEXT)");
}
推荐阅读
- typescript - TypeScript 中重载函数的类型约束
- javascript - 使用一个点 wkt 作为一个带有 openlayers 的圆
- c# - .NET StringBuilder 中出现意外的 NullReferenceException
- node.js - TypeError:foundUser 不可迭代
- laravel - 当我想更新现有数据时,在 laravel 中找不到列
- azure-devops - 如何在多阶段 YAML 管道中添加部署后批准?
- java - 当您可以使用 object.(variable_name) 调用类变量时,Java 中的构造函数有什么用?
- python - 在运行 AppEngine 服务中执行 gcloud
- javascript - 更改字体大小时的性能问题
- asp.net-core - Swagger UI 403 发布后禁止生产