Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
  }
like image 277
Hasen Avatar asked Oct 27 '25 23:10

Hasen


1 Answers

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.

like image 147
Hasen Avatar answered Oct 30 '25 16:10

Hasen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!