0

I cannot open a Sqlite3 database on my new Android app. In an effort to isolate the problem, I created a brand new app. I placed the sqlite3 db in the Assets folder under main.

In the manifest, I placed the following lines:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

I have just one activity in this test app, and this is the code in that activity. It does just one thing: attempts to open the db. It immediately dies and prints out an error to the console (details further below)

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Context context = getApplicationContext();
        String db_path = String.valueOf(context.getDatabasePath("production.sqlite3"));

        SQLiteDatabase db = SQLiteDatabase.openDatabase(db_path, null, SQLiteDatabase.OPEN_READWRITE);

    }
}

Here are the relevant errors from the log:

09-09 20:10:54.788 9850-9850/? E/SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
09-09 20:10:54.788 9850-9850/? E/SQLiteLog: (14) os_unix.c:31278: (2) open(/data/user/0/com.hawthornemackenzie.sesame/databases/production.sqlite3) - 
09-09 20:10:54.788 9850-9850/? E/SQLiteDatabase: Failed to open database '/data/user/0/com.hawthornemackenzie.sesame/databases/production.sqlite3'.
                                                 android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

It seems to be getting a valid path from the getDatabasePath call but it seems to be saying the file does not exist.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
hawmack13
  • 233
  • 2
  • 17
  • The file exists, but is malformed... `cannot open file at line`... Is it encrypted? – OneCricketeer Sep 09 '17 at 18:36
  • Have you tried this library? https://github.com/jgilfelt/android-sqlite-asset-helper – OneCricketeer Sep 09 '17 at 18:40
  • No, not encrypted. To be sure, I actually used a copy of a file that I actively use on another project. That said, I used a copy paste to put it in the asset folder in the IDE, vs moving it within the native filesystem. When I did that, I got a message that it wasn't a UTF-8 file, which isn't surprising, since it's not text. But is there a better way to move the file into assets? – hawmack13 Sep 09 '17 at 19:04
  • cricket -- i haven't tried that lib since this is so simple and I wanted to avoid more overhead and more tools. but do you think it will address this particular problem? – hawmack13 Sep 09 '17 at 19:06
  • 2
    You can put it in the assets folder, but the database file needs to eventually be moved/copied into the app's private data, I think https://stackoverflow.com/questions/20857734/reading-sqlite-file-from-asset-folder – OneCricketeer Sep 09 '17 at 20:18
  • 1
    @cricket_007 ... that was it. I had totally forgotten about that step. I copied over my dbhelper class, added the line to copy the database in Main, and voila. If you want add that as an answer, happy to accept it for you. – hawmack13 Sep 10 '17 at 12:41
  • Possible duplicate of [Reading sqlite file from asset folder](https://stackoverflow.com/questions/20857734/reading-sqlite-file-from-asset-folder) – phd Sep 10 '17 at 15:17

1 Answers1

1

If you read the logs, it does get the path, but unless you've explicitly copied the database into the app's private data folder, it can't be opened.

Also worth mentioning that assets are read only, so even if it could open the database, you'd only be able to use SELECT statements.

Refer: Reading sqlite file from asset folder

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245