2

What is the recommended way to work with multiple tables in Sqflite. Should I use one Database helper to manage all tables in one .db file. Or should I create separate Database helper for each table.

I have 8 tables to store in database. All these 8 tables are created in the different area meaning that I don't want them to be created at one. The app is being created for a shop to manage all the accounting. So there is a Suppliers table, there is a village table and so on. Also, This app will not connect to the web. It's an offline only app. So, which approach would be the best in this scenario?

1 Answers1

1

For me, I have three DB tables and like many other app developers, I just create an entirely new .db file for each table.

However, you can just combine db.execute calls and make a manager that can handle the many tables.

Example of creating many tables in one file: (Credit: How to create multiple tables in a database in sqflite?)

await db.execute('''
  create table $reminderTable (
    $columnReminderId integer primary key autoincrement,
    $columnReminderCarId integer not null,
    $columnReminderName text not null,
    $columnReminderNotifyMileage integer not null,
    $columnReminderEndMileage integer not null
   )''');
await db.execute('''
   create table $carTable (
    $columnCarId integer primary key autoincrement,
    $columnCarTitle text not null
   )''');

I would also take a look at this article: https://steemit.com/programming/@tstieff/using-sqflite-in-your-flutter-applicaiton-effectively. It uses multiple tables in one database file so you can get an example of how it would operate.

Benjamin
  • 5,783
  • 4
  • 25
  • 49
  • Can we create a separate helper for every table which has await db.execute() method to create tables when needed? Because I don't want to create all the tables at the same but at a different point of time. – Shubham Saurav Dec 18 '19 at 09:58
  • Thanks for the answer. I really appreciate it. – Shubham Saurav Dec 18 '19 at 16:11