0

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?

1
  • 1
    hive is no sql. sqlLite is sql. there is no key system. columns & rows only. in a way, column name can be considered as the key if you want. Commented Dec 15, 2022 at 8:19

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I know it is not recommended but if I have to use sqflite_chipher functionality, I have to use that option in this case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.