11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

flutter sqfliteを使って複数のテーブルを作成する方法

Posted at
class DBHelper {
  static Future<Database> databese() async {
    final dbPath = await getDatabasesPath();
    return openDatabase(path.join(dbPath, 'hogehoge.db'),
        onCreate: (db, version) async {
      return db.execute(
          'CREATE TABLE folders(id TEXT PRIMARY KEY, title TEXT, created_at TEXT)');
    }, version: 1);
  }

この方法でローカルDBを作成していたところ、複数のDBを作成する方法がわからなかった。

解決方法

class DBHelper {
  static Future<Database> databese() async {
    final dbPath = await getDatabasesPath();
    return openDatabase(path.join(dbPath, 'hogehoge.db'),
        onCreate: (db, version) async {
      await db.execute(
          'CREATE TABLE folders(id TEXT PRIMARY KEY, title TEXT, created_at TEXT)');
      await db.execute(
          'CREATE TABLE items(id TEXT, title TEXT, FOREIGN KEY(id) REFERENCES folders(id))');
    }, version: 1);
  }

returnをawaitに変えて、あとは繰り返すだけで複数のテーブルを作成することが出来た。

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?