2

I want to update an app that has been already released on Google Play, but beforehand I wanted to test and see if the local sqlite database would preserve all it's data after the update as it stores user added contents.

First I added some contents to the database on the older version and then updated it.When i updated the app through Internal testing it displayed no data as if it would have created an empty fresh database.

  • Is it possible that the db path has changed? How to check it?
  • Could it be that the older Database has been overwritten?

Is there something else that I should be on the look out for? As I have only added few more columns to the database and not changed any code regarding database initialization and databasepath.

used packages:sqflite: 2.0.0+3 and path: 1.8.0

Code snippet from the dart file for the database :

    import 'package:path/path.dart';
    import 'package:sqflite/sqflite.dart';
    import 'package:sqflite/sqlite_api.dart';
  
    class DatabaseHelper{

        /// ------- name variables ----------------
        static final _dbName = 'userTasks.db';
        static final _dbVersion = 2;                
        static final _tableName = 'myTasks';

        /// ------- table column Names -------------
        static final columnID = 'id';
        static final columnName = 'name';
        static final columnContent = 'content';
        static final columnType = 'type';
        static final columnDescription = 'description';
        static final columnFavorite = 'favorite';

        /// -------- DataBase initialization ---------
        DatabaseHelper._privateConstructor();
        static final DatabaseHelper instance = DatabaseHelper._privateConstructor();

        static Database _database;
        Future<Database> get database async{
          if(_database !=null)  return _database;

          _database = await _initiateDatabase();
          return _database;
        }

        _initiateDatabase () async{

          Directory directory = await getApplicationDocumentsDirectory();
          String path = join(directory.path,_dbName);
          return await openDatabase(path, version: _dbVersion, onCreate: _onCreate, onUpgrade: _onUpgrade);

        }

        /// ---------------    _onCreate() getting called on fresh install(no previous DB)  -----------------

        Future _onCreate(Database db, int version){
          db.execute(
              '''
              CREATE TABLE $_tableName( 
              $columnID INTEGER PRIMARY KEY,
              $columnName TEXT,
              $columnContent TEXT NOT NULL,
              $columnType TEXT NOT NULL,
              $columnDescription TEXT NOT NULL,
              $columnFavorite INTEGER)
              '''
          );
        }

        /// ---------------    _onUpgrade() getting called on _dbVersion versionChange  -----------------

        Future _onUpgrade(Database db, int oldVersion, int newVersion) {
          db.execute(''' ALTER TABLE $_tableName ADD COLUMN $columnDescription TEXT''');
          db.execute(''' ALTER TABLE $_tableName ADD COLUMN $columnFavorite INTEGER DEFAULT 0 ''');
        }


Thanks in advance!

  • a good practive creating DB tables is adding `CREATE TABLE IF NOT EXISTS ...` in case a table with that name already exists – Jorge Luis Mar 26 '21 at 04:24
  • thanks for the suggestion, now i can be at least can be certain that it doesn't overwrite/replaces the old database – Valdis Otankis Mar 30 '21 at 07:52

2 Answers2

0

I'm not sure about getApplicationDocumentsDirectory but if you use getDatabasesPath or simply a name without path and if you have auto backup on (https://developer.android.com/guide/topics/data/autobackup) it should work.

A first test could be done without google play, just keep every apk that you publish (even if you use bundle, it is always good to also build and keep an apk) so that you can test the proper migration between 2 versions.

alextk
  • 5,713
  • 21
  • 34
  • I followed your advice testing it - using apks. I tried using `getDatabasePath` aswell as only the db name. I'm quite certain that it just creates a database in another location now. When i uninstall the app and install the older apk it opens up to display all the old data previously stored, but when i update it with the newer version apk it doesn't show any old data. It only let's me create new entries. Any ideas on what to look into more? – Valdis Otankis Mar 30 '21 at 08:02
0

You can change database version and refresh application

take a look at android:allowBackup="false" android:fullBackupOnly="false" https://developer.android.com/guide/topics/manifest/application-element

and view this

Flutter how to backup and restore sqflite database?

Danilo Santos
  • 392
  • 3
  • 11