首页 > 解决方案 > Flutter,具有许多表的复杂 SQLite DB,这是最佳实践吗?

问题描述

Flutter 新手在这里害怕。

我有一个小型 Django 应用程序 (python),我将其移植到没有 Web 后端的独立 Flutter 应用程序。我直接导出了指定在我的 Django 应用程序中使用的 SQL 表的 SQL(DDL;大约 300 行),并在我的颤振应用程序中使用它(见下文)。我最终得到了大约 8 个表,我可以通过复制/粘贴 Django 通过它的ORM为我创建的 Django SQL 查询来查询这些表。

我的问题:在移动应用程序开发中拥有复杂的表格是最佳实践吗?我担心 SQLite 不适合这种复杂性。但我觉得重用这个已经生成的模型结构和 SQL 查询范围可以节省我的时间。

非常感谢,安迪。

initDb() async {
// Get a location using path_provider
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, "gear_log.db");

await deleteDatabase(path);

var theDb = await openDatabase(path, version: 1,
    onCreate: (Database db, int version) async {

      String sql = await rootBundle.loadString('assets/db/schema.txt');
      for(var s in sql.split(";")) { //seems to be a max # characters for db.execute
        if(s.length > 5) { // catching any hidden characters at end of schema
          await db.execute(s + ';');
        }
      }

      // When creating the db, create the table

    });
return theDb;

}

重用 Django 生成的 SQL 来检索数据:

Future<List<Item>> getItems() async {
var dbClient = await db;
List<Map> list = await dbClient.rawQuery('SELECT "shoe_actualpair"."id", "shoe_actualpair"."created", "shoe_actualpair"."modified", "shoe_actualpair"."name", "shoe_actualpair"."shoe_id", "shoe_actualpair"."expires", "shoe_actualpair"."runner_id" FROM "shoe_actualpair" WHERE "shoe_actualpair"."runner_id" = 1 ORDER BY "shoe_actualpair"."modified" DESC, "shoe_actualpair"."created" DESC');
List<Item> employees = new List();
for (int i = 0; i < list.length; i++) {
  employees.add(Item.fromMap(list[i]));
}
return employees;

}

标签: djangosqliteflutter

解决方案


您可以使用捷豹 ORM。https://github.com/Jaguar-dart/jaguar_orm

我在具有一对一、一对多和多对多关系的应用程序中使用它。

对于 sqlite (sqflite),您还需要在您的颤振应用程序中使用此适配器: https ://github.com/Jaguar-dart/jaguar_orm/tree/master/sqflite


推荐阅读