Attempt to write a readonly database SQLITE, Dart and Flutter

Viewed 32

I'm trying to update a table from my data sqlite data base.The data base “AlbumChecker.db” is stored in the assets directory of the flutter project. When the data base is initiated, I’m making a copy of the assets data base to the internal device storage.The data base loads normally but when trying to update it with new information a “Attempt to write a readonly database” message appears in the console.

class DBProvider extends ChangeNotifier{
    static Database? _database;
    static final DBProvider db = DBProvider._();

    DBProvider._();

    DBProvider();

    Future<Database> initializeDatabase() async {
         var databasesPath = await getDatabasesPath();
         var path = join(databasesPath, "AlbumCheckerDB.db");
         print(path);

// Check if the database exists
         var exists = await databaseExists(path);

         if (!exists) {
            // Should happen only the first time you launch your application
            print("Creating new copy from asset");

            // Make sure the parent directory exists
            try {
              await Directory(dirname(path)).create(recursive: true);
            } catch (_) {}

            // Copy from asset
            ByteData data = await rootBundle.load(join("assets",      "AlbumCheckerDB.db"));
            List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);

            // Write and flush the bytes written
            await File(path).writeAsBytes(bytes, flush: true);
            } else {
               print("Opening existing database");
            }
            // open the database
            var bomDataTable = await openDatabase(path, readOnly: true);

            _database = bomDataTable;

            return bomDataTable;
      }

      //Update a player data
      Future<int> updatePlayer (PlayerModel playerModel) async {
      try {
         final db = await _database;
         final response = await db!.update(
         'Players', playerModel.toJson(), where: 'id = ?',
         whereArgs: [playerModel.id]);
         return response;
         }catch(e){
            print(e);
            return 0;
         }
     }




}

You can take a look at the hole project here: https://github.com/juanfranciscocis/Album_Checker

0 Answers
Related