Home » Android » android – unknown error (code 14): Could not open database

android – unknown error (code 14): Could not open database

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a working app which employs a database and SQLiteAssetHelper. The database file (sql.sqlite) is zipped (sql.sqlite.zip) and put in the assets/databases folder.

The program works perfectly well. And when run for a second/third/fourth… time it starts up quickly. But if I do a “force stop” followed by a “clear data” then run it again, then when I run it (viewing logs) I see an error “unknown error (code 14): Could not open database”… but then I wait a few seconds and it all loads and works perfectly.

The log is as follows:

W/SQLiteAssetHelper(18393): could not open database SQL.sqlite - unknown error (code 14): Could not open database
W/SQLiteAssetHelper(18393): copying database from assets...
W/SQLiteAssetHelper(18393): extracting file: 'sql.sqlite'...
W/SQLiteAssetHelper(18393): database copy complete
I/SQLiteAssetHelper(18393): successfully opened database SQL.sqlite

So it looks like it somehow tried to find the database, failed, crashed, and then repaired itself. So my question is, how could I have avoided the clumsy start and the several seconds of delay?

EDIT: My first reference to the database in my code is…

public class Globals extends Application
{
    Custom_SQLiteAssetHelper db;   
    public void onCreate()
    {
        super.onCreate();
        db = new Custom_SQLiteAssetHelper(this);
        cursor = db.get_first_species_common_name();

and I have…

public class Custom_SQLiteAssetHelper extends SQLiteAssetHelper
{
    public Custom_SQLiteAssetHelper(Context context)
    {
        super(context, "SQL.sqlite", null, 1);  
    }
    public Cursor get_first_species_common_name()
    {
        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder querything = new SQLiteQueryBuilder();
            // etc...
How to&Answers:

Finally I have found the answer. you should use the newer version of SQLiteAssetHelper

Answer:

Try to change

super(context, "SQL.sqlite", null, 1);

with

 super(context, "SQL", null, 1);

Answer:

public Custom_SQLiteAssetHelper(Context context)
{
    super(context, "SQL.sqlite", null, 1);  
}

Change to

public Custom_SQLiteAssetHelper(Context context)
{
    super(context, "SQL.sqlite", null, 1);  
    close();
}

@haike00 he is using SQLiteAssetHelper which is a library, it support you copy pre-filled sqlite database to internal/external storage when first run app. When using this lib, you have to zip your sqlite file, no other options.