django - 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;
}
解决方案
您可以使用捷豹 ORM。https://github.com/Jaguar-dart/jaguar_orm
我在具有一对一、一对多和多对多关系的应用程序中使用它。
对于 sqlite (sqflite),您还需要在您的颤振应用程序中使用此适配器: https ://github.com/Jaguar-dart/jaguar_orm/tree/master/sqflite
推荐阅读
- php - 如何在 Laravel 7.x 中从控制器返回视图时传递 cookie?
- swift - 时间来错了,但日期很快就正确了
- javascript - 更改 NavLink 文本颜色 onClick
- azure - 与美国西部的服务总线通信时,英国南部的 Azure 功能超时
- java - 无法执行休眠查询语句?
- html - 之间的关系
和 HTML5 中的标签
- python - 为什么我只需要从模块导入子类而不从另一个实例导入它的基类和它的属性?
- api - 我们如何测试使用 azure 持久函数的 rest api 端点?
- html - 无法发布用 MD 文件写入的表格
- css - 我想用 sass 将图标悬停为白色(没有过滤器)