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?
CodePudding user response:
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.