4

An empty database file with the same name exists BEFORE we copy it from the assets folder.

android.database.sqlite.SQLiteException: no such table:

This error message is legitimate because an empty database exists in our data directory with the same name. Although we have not copied it yet.

How is our database file being created?

Why does it exist even if we have not called or instantiated our Database Helpers?

Using the same SQLiteHelper classes for all previous versions with no issues. Running on Android 9 Pie causes this error message after a clean install of our Android app that uses an existing database in the assets directory. Normally when this file does NOT exist in the data directory it is copied then opened.

Checked and rechecked, the file exists BEFORE our call to move it from our assets directory.

When checking the Android 9 Pie data directory, our SQLite database file already exists. This is after a new install AND clearing cache & data

To repeat what we are seeing.

  1. Settings… Apps & Notifications
  2. Select our app
  3. Storage
  4. Clear Cache + Clear Storage
  5. Uninstall the app.

    Launcher Activity "SplashActivity" onCreate()

    String mName = "myawesomedatabase.db";
    String mDatabasePath = this.getApplicationInfo().dataDir + "/databases";
    
    
    File file = new File (mDatabasePath + "/" + mName);
    Log.i("DATABASE", "##### SplashActivity.getData() " + mDatabasePath + "/" + mName);
    if (file.exists()) {
       Log.i("DATABASE", "##### SplashActivity.getData() FILE EXISTS!!!");
    
    }
    

Repeating the above and NOT uninstalling the app, copies the file properly. It is ONLY on new installations.

We have confirmed we are not hardcoding the data directory. Researching this we learned that the database directory has changed.

From: /data/data/com.___._____/databases/

To: /data/user/0/com.____.____/databases/

Referencing this article that helped this discovery. Android P - 'SQLite: No Such Table Error' after copying database from assets

Work around:

  1. Clean install of the app.

  2. Wait for crash

  3. Settings... Apps... Specific App... Clear Cache + Clear Storage

  4. Run app again

  5. Success, database does NOT exist in data directory, then the app copies file from Assets Directory with no issue.

Berry Wing
  • 113
  • 1
  • 5

1 Answers1

2

No stupid questions? But man I feel dumb when it was an easy answer.

Perhaps taking the time to ask here made us look harder.

Android P has auto restore from backup. Even if you do not have a copy of the app installed. So a corrupted file was being restored from backup.

Android P steps to correct (at least in development environment)

  1. Settings...

  2. System

  3. / Advanced

  4. Backup

  5. App data

  6. Turn OFF Automatic Restore

Discovery came after finding out it worked on AVD with Factory reset. However development devices with different app versions experienced the same issue.

Berry Wing
  • 113
  • 1
  • 5