Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can I save data with key in sqflite package in flutter?

Actually, I know it is possible to do that on hive package in flutter, but I am wondering it is possible to do that on sqflite package?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The sqflite package uses SQLite, which is a relational database, not a key-value pair database. But you can technically replicate the behavior of a key-value database in it by creating a single table with all the data.

Please note that this is NOT a recommended way to use SQL. If you want a key-value db, just use hive.

First, create the database with the table

Database database = await openDatabase(path, version: 1,
  onCreate: (Database db, int version) async {
    // When creating the db, create the table
    await db.execute(
      'CREATE TABLE Data (_id INTEGER PRIMARY KEY, key TEXT, value TEXT)');
});

Putting the data into the database:

await database.transaction((txn) async {
  await txn.rawInsert(
      'INSERT INTO Data(key, value) VALUES("key", "value")');
});

Getting data from the database:

String value = await database.rawQuery('SELECT "key" FROM Data').first['value'];

Again, this is NOT recommended. Use Hive or other key-value db.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading