17

I am trying to copy an existing database from my assets folder and execute some operations on it. Everything is working fine but I've gotten the following error in the log files of my emulator:

sqlite returned: error code = 14, msg = cannot open file at source line 25467

09-06 11:23:41.844: INFO/Database(22560): sqlite returned: error code = 14, msg = cannot open file at source line 25467
09-06 11:23:41.885: ERROR/Database(22560): sqlite3_open_v2("/data/data/com.dhani.Lazy/databases/LazyDB.sqlite", &handle, 1, NULL) failed
09-06 11:23:41.885: WARN/System.err(22560): android.database.sqlite.SQLiteException: unable to open database file
09-06 11:23:41.894: WARN/System.err(22560):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
09-06 11:23:41.904: WARN/System.err(22560):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1849)
09-06 11:23:41.914: WARN/System.err(22560):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
09-06 11:23:41.914: WARN/System.err(22560):     at com.dharani.LazyApple.Database.DataBaseHelper.checkDataBase(DataBaseHelper.java:72)
09-06 11:23:41.914: WARN/System.err(22560):     at com.dharani.LazyApple.Database.DataBaseHelper.createDataBase(DataBaseHelper.java:47)
09-06 11:23:41.914: WARN/System.err(22560):     at com.dharani.LazyApple.Database.DataBaseHelper.Login(DataBaseHelper.java:166)
09-06 11:23:41.914: WARN/System.err(22560):     at com.dharani.LazyApple.Views.LoginView$1.onClick(LoginView.java:63)
09-06 11:23:41.934: WARN/System.err(22560):     at android.view.View.performClick(View.java:2485)
09-06 11:23:41.934: WARN/System.err(22560):     at android.view.View$PerformClick.run(View.java:9080)
09-06 11:23:41.944: WARN/System.err(22560):     at android.os.Handler.handleCallback(Handler.java:587)
09-06 11:23:41.944: WARN/System.err(22560):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-06 11:23:41.944: WARN/System.err(22560):     at android.os.Looper.loop(Looper.java:123)
09-06 11:23:41.944: WARN/System.err(22560):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-06 11:23:41.964: WARN/System.err(22560):     at java.lang.reflect.Method.invokeNative(Native Method)
09-06 11:23:41.964: WARN/System.err(22560):     at java.lang.reflect.Method.invoke(Method.java:507)
09-06 11:23:41.964: WARN/System.err(22560):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-06 11:23:41.964: WARN/System.err(22560):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-06 11:23:41.964: WARN/System.err(22560):     at dalvik.system.NativeStart.main(Native Method)

Any suggestions on how to solve this problem?

2
  • Are you sure you have the file "/data/data/com.dhani.Lazy/databases/LazyDB.sqlite"? Commented Sep 6, 2011 at 8:19
  • @LAS_VEGAS yes, have the file at "/data/data/com.dhani.Lazy/databases/LazyDB.sqlite" and i can also execute SQLite Queries on it. everything thing is working but in log the above error shows up. Commented Sep 6, 2011 at 8:51

11 Answers 11

10

This may be a little late, but hope this helps for whoever gets this problem (since I can't find a definitive solution around).

I think I know the reason for this cause (at least for my case). Looking in the DDMS --> File Explorer, you'd realize that the Database Folder (/data/data//databases/) does not exist, which is why the application cannot create the database file in that non-existent folder. If you can create a databases folder in some manner, you can avoid this problem.

Because I'm lazy, I just used the /data/data//files/ folder when I'm in Emulator mode. You can get the files dir using this:

context.getFilesDir().getPath()

This worked beautifully for me in the Emulator.

Hope this helps someone.

In case you want to see some code:

String dbFilename = "example.db";
try
{       
    File databaseFile = getDatabasePath(dbFilename);        
        SQLiteDatabase _db = SQLiteDatabase.openOrCreateDatabase(databaseFile);
} catch (Exception e)
{
    String databasePath =  getFilesDir().getPath() +  "/" + dbFilename;
    File databaseFile = new File(databasePath); 
    _db = SQLiteDatabase.openOrCreateDatabase(databaseFile);
}

EDIT: I tried logging into Facebook (my app has FB integration) on the Emulator and /databases folder appeared after that (and persisted). Not sure what happened, but it's possible to create that folder somehow. Something for another expert around here to shed light on.

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

2 Comments

i have same logcat ... check your db name doesn't have caps characters in his string
Thanks for the File Explorer idea! My code 14 was due to the database file name already existing as a directory!? I had followed the documentation tutorial to the letter and called mkdirs() every time…
6

I have seen this error occur if you are using sharedUserId in your manifest. If you change the sharedUserId of an application and reinstall the application it does not have the required ownership to write to the SQLite database.

1 Comment

this is my manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="schemas.android.com/apk/res/android" package="com.dhani.Lazy" android:versionCode="1" android:versionName="1.0"><application -----><activity ----/></application>
6

Today this issue cost me 3 hours. What I tried:

  • Rewriting the copy database code.
  • Deleting the app from the emulator / device
  • Wiping emulator(s)
  • Cleaning eclipse
  • Changing file permissions

I solved the problem by copying the code from a shared Dropbox account to another location and refactoring the code in the Android Manifest and java files with another package name.

The application runs beautifully now.

1 Comment

Sounds like you could have saved some time by simply doing a clean.
6

I had the same problem.

What solved it, was replacing

private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";

with

String myPath = myContext.getFilesDir().getAbsolutePath().replace("files",
"databases")+File.separator + DB_NAME;

3 Comments

My db is in "/data/data/YOUR_PACKAGE/files/". Does it have to be in a "databases" subfolder?
Yes, put it in 'databases' folder
I finally got mine to work. wanted to circle back that it works fine in the "files" subfolder.
3

I had the same error code on logcat:

sqlite returned: error code = 14, msg = cannot open file at source line

The app would run fine (I actually didn't notice any issues until I looked at logcat!). Logcat informed me of this error so I thought it would be best to get this resolved now.

It turns out that if I edited the manifest file it got rid of the error! Within my manifest I had typed in the incorrect min sdk version. I discovered this because within the manifest file there was an exclamation indicator along the left column of the code informing me of my mistake.

To be thorough, I would recommend that you should make sure all errors/exclamations have been fixed so you can at least rule them out.

It seems this error (and many more in eclipse) can occur depending on a vast number of factors! Oh the joys!

Comments

3

Or, another easy solution that worked for me is as follows :

  1. In emulator's settings option, click Application
  2. Go to Manage Applications
  3. Find your App, and click Uninstall
  4. Then run your App again from the Eclipse.

1 Comment

ok ... which way to for uninstall and install every execution debug mode?
2

My reason was different: I've accessed online version of the app, which created the database. The PhoneGap application then couldn't access the file created by different app. Clearing browser cache solved my problem.

Comments

2

The enigma is solved, tested in the emulator without the DB and mark them wrong, so you must first create the DB

    InputStream myInput = myContext.getAssets().open(DB_NAME);

    //Destination folder (where we created the DB empty)
    String outFileName = DB_PATH + DB_NAME;

    //We opened it BBDD Vacia as OutputStream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //Transfers Bytes between input and output Stream
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the open files
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

After that, do your operations. Good luck!

Comments

2

I managed to solve this by realizing that the documentation is slightly misleading. You need to have a file name for your zipped asset that is related to the name of your database. For instance, if your database is called someData.db, your zip file must be someData.zip.

Also, there seems to be a bit better diagnostics if you try opening a writable db. I did in the constructor, as follows, which was what ultimately helped me to figure out the solution.

SQLiteDatabase db=getWritableDatabase();
db.close();

1 Comment

i rename my db that has CAPS char in first letter? and resolved.. this is true?
2

Make sure you are closing all your cursor and db instances. I had this error due to making multiple inserts and updates and not closing the cursors, causing cursor leaks, and unable to open the db.

Comments

0

The database directory is not created automatically on some devices. You just need to detect missing databases directory and create it:

File databaseFile = getDatabasePath("my.db");

// Create directory when needed
File databaseDir = databaseFile.getParentFile();
if(!databaseDir.exists()) {
    databaseDir.mkdirs();
}

SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.