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.

7
  • The file exists, but is malformed... cannot open file at line... Is it encrypted? Commented Sep 9, 2017 at 18:36
  • Have you tried this library? github.com/jgilfelt/android-sqlite-asset-helper Commented Sep 9, 2017 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? Commented Sep 9, 2017 at 19:04
  • 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 stackoverflow.com/questions/20857734/… Commented Sep 9, 2017 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. Commented Sep 10, 2017 at 12:41

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.