Flutter, complex SQLite DBs with many tables, is this best practice?
You can use jaguar ORM. https://github.com/Jaguar-dart/jaguar_orm
I am using it in a an app with both one-one, one-many and many-many relationships.
For sqlite (sqflite), you also need this adapter in your flutter app: https://github.com/Jaguar-dart/jaguar_orm/tree/master/sqflite
andyw
Updated on December 07, 2022Comments
-
andyw over 1 year
Flutter newbie here afraid.
I have a small Django app (python) that I am porting over to a standalone Flutter app with no web back-end. I directly exported the SQL (DDL; about 300 lines worth) that specifies my SQL tables used in my Django app and use that in my flutter app (see below). I end up with ~8 tables and I can query these by just copy/pasting the Django SQL queries Django creates for me via it's ORM.
My question: is it best practice to have complex tables in mobile app development? I worry SQLite is not best suited for such complexity. But I feel it saves me time to reuse this already generated model structure and range of SQL queries.
Many thanks, Andy.
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;
}
Reusing Django generated SQL to retrieve data:
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;
}