4

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);
  }
Hasen
  • 11,710
  • 23
  • 77
  • 135
  • by calling: `await db.execute(...); await db.execute(...);` ? – pskink Nov 03 '19 at 10:48
  • @pskink I tried that and it doesn't work. The second database is not found. – Hasen Nov 03 '19 at 11:03
  • @pskink You can't do `return await db.execute` anyway though. – Hasen Nov 03 '19 at 11:05
  • @pskink It definitely works as it is but it only creates one database. – Hasen Nov 03 '19 at 11:05
  • why not? ofcourse you can – pskink Nov 03 '19 at 11:05
  • @pskink To be honest I don't know what your code actually is. Can you write the entire function out? – Hasen Nov 03 '19 at 11:07
  • what you dont know? `await` keyword? simply call two `db.execute` statements and wait for them to be finished by adding `await` keyword – pskink Nov 03 '19 at 11:08
  • `await` is already in the function?? Not sure what you're saying, you're just saying bits and pieces and so I've no idea what your actual whole function is. – Hasen Nov 03 '19 at 11:09
  • do you see `final dbPath = await sql.getDatabasesPath();` in your code? do the same with `db.execute` – pskink Nov 03 '19 at 11:10
  • @pskink No idea what exactly you want to say...please write out the function or at least the code within the return statement. – Hasen Nov 03 '19 at 11:11
  • i think that first you need to read [Asynchronous programming: futures, async, await](https://dart.dev/codelabs/async-await) - it explains how and when to use `await` keyword – pskink Nov 03 '19 at 11:12
  • @pskink Again the function is **already** asynchronous, not sure why you think I don't know that that means. Anyway, I think we're looking at different things. – Hasen Nov 03 '19 at 11:13
  • @pskink You obviously know what you mean but it's not guaranteed that that even works. Unless you can write clearly what you're saying I can't tell what you mean. – Hasen Nov 03 '19 at 11:14
  • https://github.com/tekartik/sqflite/blob/master/sqflite/example/lib/type_test_page.dart#L118 here you have how to use `await` and `db.execute`, and here: https://github.com/tekartik/sqflite/tree/master/sqflite/example/lib you have more examples – pskink Nov 03 '19 at 11:17
  • @pskink I got it to work in the end. There's no need for another `async` `await`, I just returned two `db.execute` from a separate function. 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. – Hasen Nov 03 '19 at 11:27
  • yes, you have to await for every `db.execute` - otherwise the strange race condition can happen – pskink Nov 03 '19 at 11:32
  • @pskink No, I said there was no need for an additional `async` `await` since the function already has it. Check my answer below. – Hasen Nov 03 '19 at 11:38
  • @pskink There's no need for `await` with `db.execute`. Try it yourself. – Hasen Nov 03 '19 at 11:39

1 Answers1

7

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.

Hasen
  • 11,710
  • 23
  • 77
  • 135