Android 如果sqflite颤振中存在,则删除表格并重新创建
我想在填充服务器数据之前删除表(如果存在) 我正在尝试的问题Android 如果sqflite颤振中存在,则删除表格并重新创建,android,dart,flutter,sqflite,Android,Dart,Flutter,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 =optio
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');
}
void\u插入选项(选项GroupList选项)异步{
int idd=dbHelper.dropTable();
打印('DROP TABLE:$idd');
PreferencesConnector myprefs=PreferencesConnector();
String merchantid=wait myprefs.readString('merchantid');
String hashkey=await myprefs.readString('hashkey');
地图行={
DatabaseHelper.columnGroupId:option.grouprowid,
DatabaseHelper.columnGroupName:option.groupname,
DatabaseHelper.columnIsRequired:option.isrequired,
DatabaseHelper.columnMerchantId:merchantid,
DatabaseHelper.ColumnMerchantAshKey:hashkey,
};
int id=await dbHelper.insertOption(行);
打印('插入选项行id:$id');
}
实际上,您的dropTable方法似乎并没有实际删除任何表:
要删除(删除)表格,请执行以下操作:
DROP TABLE IF EXISTS my_TABLE
要清除表格内容,请执行以下操作:
从my_表中删除
正如问题中所问,我们需要一种方法来删除一个表(如果存在),并在flift中使用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)");
}
Future DropTableIfExistsInCreate()异步{
//这里我们通过调用openDatabase方法来获取数据库对象
//它接收路径和onCreate函数以及所有好东西
Database db=等待openDatabase(路径,onCreate:…);
//这里我们执行一个查询来删除表(如果存在),该表称为“tableName”
//也可以作为方法的输入参数
等待db.execute(“如果存在表名,则删除表”);
//最后,在这里,我们重新创建我们喜爱的“tableName”,它需要
//某些列初始化
等待db.execute(“创建表tableName(id整数,name TEXT)”;
}
也许已经太晚了,但如果这对某人有帮助的话。
要从表中删除所有行,请使用
final db = await database;
db.delete("tableName");
奇怪,但是如果您等待delete方法的结果,它将不起作用。不理解Flatter,但。。。dropTable()中的select语句将删除该表???@AIMINPAN我无法删除该表。是否尝试了
int idd=await dbHelper.dropTable()代码>?我假设这是异步的,虽然这段代码可以解决这个问题,但它如何以及为什么解决这个问题将真正有助于提高您的帖子的质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。