How to connect SQLite database in Flutter

Viewed 26

I have SQLite dababase in my assets folder. How to connect the existing SQLite database to Flutter and get all items from the table?

1 Answers

To connect in existing database is :

var dbDir = await getDatabasesPath();
var dbPath = join(dbDir, "app.db");


final myDB = await rootBundle.load('assets/myDb.SQLITE');
List<int> bytes = myDB.buffer.asUint8List(myDB.offsetInBytes, myDB.lengthInBytes);
await File(dbPath).writeAsBytes(bytes);

var db = await openDatabase(dbPath);

can you read here : https://stackoverflow.com/a/53128435/10649115

And for a complete example you can see this gist: https://gist.github.com/sergiotucano/57be4db96bfa5d23d68d242d392a9f7d

Related