Flutter SQFLite 一次创建多个表
Flutter SQFLite create multiple tables at once
I'm using the code below and want to know how this database function can be altered so that it creates two separate tables at once:
static Future<Database> database() async {
final dbPath = await sql.getDatabasesPath();
return sql.openDatabase(path.join(dbPath, 'mydatabase.db'), onCreate: (db, version) {
return db.execute('CREATE TABLE mytable(date TEXT PRIMARY KEY, value DOUBLE)');
}, version: 1);
}
我是这样自己解决的:
static Future<Database> database() async {
final dbPath = await sql.getDatabasesPath();
return sql.openDatabase(path.join(dbPath, 'mydatabase.db'), onCreate: (db, version) => _createDb(db), version: 1);
}
static void _createDb(Database db) {
db.execute('CREATE TABLE mytable(date TEXT PRIMARY KEY, value DOUBLE)');
db.execute('CREATE TABLE mytableb(date TEXT PRIMARY KEY, value DOUBLE)');
}
它不工作的原因是因为删除原始数据库后我需要从冷启动模拟器才能生效。
I'm using the code below and want to know how this database function can be altered so that it creates two separate tables at once:
static Future<Database> database() async {
final dbPath = await sql.getDatabasesPath();
return sql.openDatabase(path.join(dbPath, 'mydatabase.db'), onCreate: (db, version) {
return db.execute('CREATE TABLE mytable(date TEXT PRIMARY KEY, value DOUBLE)');
}, version: 1);
}
我是这样自己解决的:
static Future<Database> database() async {
final dbPath = await sql.getDatabasesPath();
return sql.openDatabase(path.join(dbPath, 'mydatabase.db'), onCreate: (db, version) => _createDb(db), version: 1);
}
static void _createDb(Database db) {
db.execute('CREATE TABLE mytable(date TEXT PRIMARY KEY, value DOUBLE)');
db.execute('CREATE TABLE mytableb(date TEXT PRIMARY KEY, value DOUBLE)');
}
它不工作的原因是因为删除原始数据库后我需要从冷启动模拟器才能生效。