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);
}
I managed to solve it myself like this:
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)');
}
The reason it wasn't working was because after deleting the original database I needed to restart the simulator from cold for it to take effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With