如何在Flutter中的不同时间在Sqlite数据库中创建多个表

时间:2019-10-15 08:16:26

标签: database sqlite flutter sqflite

因此,最初,我在数据库中创建了一个名为“ TABLE”的表。我想添加另一个表。因此,我添加了另一个查询来创建表。但是,当我运行该应用程序时,出现错误消息“ TABLE1不存在”。

我确实感觉我的代码有缺陷。我认为_onCreate()方法只会在我第一次运行应用程序时被调用。因此,之后添加到_onCreate()方法的任何代码都不会运行。任何帮助将不胜感激。

     class DBHelper {
       static Database _db;
       static const String DB_NAME = 'employeeDB';

       static const String ID = 'id';
       static const String NAME = 'name';
       static const String TABLE = 'Employee';

       static const String ID1 = 'id1';
      static const String NAME1 = 'name1';
     static const String TABLE1 = 'Employee1';


   Future<Database> get db async {
if (_db != null) {
  return _db;
}
_db = await initDb();
return _db;
   }

 initDb() async {
 io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, DB_NAME);
var db = await openDatabase(path, version: 1, onCreate: _onCreate);
return db;
  }

 _onCreate(Database db, int version) async {
 await db.execute("CREATE TABLE $TABLE ($ID INTEGER PRIMARY KEY,$NAME TEXT)");
await db.execute("CREATE TABLE $TABLE1 ($ID1 INTEGER PRIMARY KEY,$NAME1 TEXT)");

  }

   Future<Employee> save(Employee employee) async {
var dbClient = await db;
employee.id = await dbClient.insert(TABLE, employee.toMap());
return employee;

  }

  Future<Employee1> saveEmp1(Employee1 employee) async {
var dbClient = await db;
employee.id = await dbClient.insert(TABLE1, employee.toMap());
return employee;

  }


   Future<List<Employee>> getEmployees() async {
var dbClient = await db;
List<Map> maps = await dbClient.query(TABLE, columns: [ID, NAME]);


List<Employee> employees = [];
if (maps.length > 0) {
  for (int i = 0; i < maps.length; i++) {
    employees.add(Employee.fromMap(maps[i]));

  }
  }


return employees;
}


Future<List<Employee1>> getEmployees1() async {
var dbClient = await db;
List<Map> maps = await dbClient.query(TABLE1, columns: [ID1, 
 NAME1]);


List<Employee1> employees = [];
if (maps.length > 0) {
  for (int i = 0; i < maps.length; i++) {
    employees.add(Employee.fromMap(maps[i]));

  }
}
return employees;
   }

  }

3 个答案:

答案 0 :(得分:2)

首次在模拟器中运行此应用程序和initDb()时,数据库文件employeeDB已创建。
并且不会再次创建

对于仅测试应用执行, 您可以更改

String DB_NAME = 'employeeDB'

改名

String DB_NAME = 'employeeDB1'

或者您可以先从模拟器中卸载此应用,然后再次运行。

https://github.com/tekartik/sqflite/blob/master/sqflite/lib/sqlite_api.dart的源代码段

/// Called when the database is created.
  OnDatabaseCreateFn onCreate;

对于架构迁移,您可以使用OnUpgrade,详细参考https://efthymis.com/migrating-a-mobile-database-in-flutter-sqlite/

代码段

await openDatabase(path,
  version: 1,
  onCreate: (Database db, int version) async {
    await db.execute(initialSchema));
  },
  onUpgrade: (Database db, int oldVersion, int newVersion) async {
    await db.execute(migrationScript));
  });

答案 1 :(得分:1)

您必须创建迁移,并使用onUpgrade处理程序对现有数据库运行迁移。基本上,您需要检查现有数据库版本并升级,如果该版本小于迁移号。

您可以查看详细步骤/代码教程here

答案 2 :(得分:0)

我不仅需要创建多个表,还需要创建多个表。但是多个数据库。从那以后,我开发了飞镖包。 sqlite_at_runtime可以扩展到在运行时创建所有这些实体。

下面是一个示例代码,该示例创建了三个具有相似属性的表。

await Sqlartime.tableCreate(['sample1','sample2','sample3'],['name TEXT','age INTEGER','temp REAL']);