Android sqlite unable to open database file

SQLite unable to open database file (code 14) on frequent «SELECT» query

I have following class «Singleton» to handle SQLite connection and to make sure to have 1 instance of connection for whole process/app:

And DBHelper class itself:

When I frequently try to «SELECT» some info from database I am receiving following error:

I am running following code to execute query:

How can I manage/improve my database connection to overcome/avoid this problem?

5 Answers 5

before executing any query to this stuff (You should open your database). Close db after completion the task.

// Then write your query now. and then close the db.

You can check out my git link1 git link2

As an addition, I believe that having too many cursors, and thus open’s, can also result in the same «unable to open database file error». (in the following code, there’s 507 rows in the shoplistcursor , so that was over 1500 cursors used/reused in total)

I was getting the same message. As per :-

The code, that was in error, was :-

The cure was to close the cursors on each iteration. As per :-

Your code re-opens the database every time dbOpen() is called.

An SQLite database object is quite lightweight; it does not really make sense to keep closing and re-opening it.

You already have your singleton; just store a single SQLiteDatabase reference there.

Источник

Random exception android.database.sqlite.SQLiteException: unable to open database file

My app uses a uncaught exception handler that sends the stack trace to me when the app crashes. Often I get this report from random users.

I cannot replicate it, the opening of the database always succeeds in my case. This is not a database stored on external SD card, only a database opened with SQLiteOpenHelper(context, «SomeName», null, someVersionCode) .

Do you have any experience with this? What are the possibilities that I can check before opening the database?

7 Answers 7

i got the same error after upgrading my android firmware. the database was created by the old firmware and therefore couldn’t be opened by the new firmware. users could solve this error by uninstall and reinstall your app.

One of the possible scenarios when this could happen — is when you access your database file from several threads and when the file is locked by one of the threads while you’re trying to open it for modifications from another thread.

there can be if your app opens database from multiple threads at same time that can be the reason for this exception.

Читайте также:  Обои для андроид мустанг

please try synchronized method to open database.synchronized method will not allow database to be open from multiple threads at same time. put below code in your dbHelper

and call these method when your are getting db. Hope it will help.

This kind of issue i have faced, I have solved this issue like that way.

We need check whether Old database is same as New Database, If something changed into new Database that time we need delete Old database and install new database OR alter the table as per new Database

Second option is easy just uninstall your old application and than install new application

Hope this will work for you.

For any database application , I create the sqlite database first with Sqlite Manager addon of Firefox and take it in /res/raw folder. Then in my code, I check if the database exists in /data/data for this app. If not , I copy the db file to that directory with my code.

For your situation, it’s hard to determine the exact reason without seeing how you have created your database and how are you using it.There can be several reason for the issue of getting this exception. Some issues already mentioned by others. I want to mention a one. Can you try opening the database like this

Probably, I could try better to help if you show your code snippets. Well, my methods are described here , you can take a look if that helps.

I have faced this issue.

One of my applications was behaving in the same way, because I added the code to copy database from assets to specified location on the splash screen (the screen which is visible when app launches and automatically goes off the view after few seconds).

And what was happening in few cases, that database was not getting copied in the specified time frame and accessing it from within the app was causing exceptions.

I just wrote the code to copy database on background thread and show the splash screen to the user till the time database is not copied only after that show the next screen.

There could be another solution in case, if the app is being installed on previous version, write the code to copy the saved data from previous version of app to new vesrion if data prevention is necessary and new version contains different models than that of previous version.

Источник

SQLite Android cannot open database file

Edit: I tried this on my phone and it works, can anyone tell me why it does not work on an emulator?

I am trying to open a database on android, but it is throwing an «Database file could not be opened» Exception. In the debugger, it seems that the error is occurring on the line mDb = mDbHelper.getWritableDatabase();

Читайте также:  Картридер флешка для андроид

My code is as follows:

Does anyone know what the problem is?

5 Answers 5

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:

This worked beautifully for me in the Emulator.

Hope this helps someone.

In case you want to see some code:

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.

Although not relevant for your current issue, the onUpgrade method in SQLiteOpenHelper is also bound to fail in the future: If the action you want to take when a new version of the db is deployed, is to start from scratch, you will need to delete the old tables first.

Edit After some research I found out that your problem is rather common:

the 3 links provided with this answer

are particularly interesting and provide some kind of workaround, should you not find a clean way to make the bug disappear.

That said if I where in you, as a first attempt to solve your problem before delving with complex workarounds, I would try to update all Android Development Tools (the SDK in particular, but updating the eclipse plugin won’t hurt), completely remove your app from both the emulator and devices, and run it again.

Edit 2 Disregard the following remark (it’s not correct, I leave it here for reference)

Your code doesn’t work because the SQL query is wrong: you spelled

where it should have been

(besides, it happens to me too that I get crazy with the Android db classes only to find that the problem resides in the SQL queries 🙂 )

Источник

«Unable to open database file» when using SQLiteOpenHelper with instrumentation context

I’m trying to create a database that contains mock objects for testing purposes. I’d prefer to have this db reside in my test package instead of in my application package.

When I create my SQLiteOpenHelper from within my app, I pass in the instrumentation context as well as a path that’s relative to my test package’s database directory. I would think this would allow me to create a database in my test package, but this doesn’t seem to be the case. The «databases» directory is never created in my test package.

Читайте также:  Как удалить аккаунт dropbox с андроида

Is there a way to have my app create and access a database that resides in my test project?

2 Answers 2

I wasn’t sissified with @Yves solution because it results in databases created in tested appliction’s folder. This can potentially interfere with the application (like purging databases during tests).

I’ve made some investigations. This error is thrown because «databases» directory fails to be created, and when SQL engine tries to open «databases/dbname.db» file it can’t find «databases» folder.

Usually Context is responsible for creating «databases» folder just before creating database for the first time. But it fails because it has invalid permissions. And here is why.

Let’s assume Tested project is installed with user app_100, and Tester project is installed with user app_101. When tests are running, the Instrumentation runs using app_100 user, and not app_101. And app_100 has not right to create folder in app_101 private folder.

Источник

SQLite & Android: unable to open database file — error 14

I saw that a similar issue has been reported many times. In my case, I’m directly using the «C» API invoked using my own JNI library (not the Android Java layer), with the latest amalgamation 3.8.3. So I control what I’m dealing with. Then, on certain operations (UPDATE) I’m getting an error stating that it cannot open a database file. I don’t get that with INSERT statements, only UPDATE. When I activate the SQLite error callback, I’m seeing the following:

cannot open file at line 29299 of [6c643e45c2] os_unix.c: 29299: (13) open(./etilqs_1zMsiYdpXhd3JqY) — statements aborts at 36: [UPDATE . ]

Digging this a little bit further, it appears to be related to the journal file. When I set the journal_mode to MEMORY using a pragma, then the issue disappears. But all the other modes, like WAL or TRUNCATED lead to the same error.

When debugging the code from eclipse, I can see the -journal file being created when performing the INSERT. And then being removed when the transaction is complete. So it tells me that it has enough rights on the directory (/data/data//)

Note that the exact same code works perfectly (C & Java) on Windows, where I’m not getting the error. There is definitively something going on with Android and file access.

I have a way to get the error reproduced systematically. If someone has some idea on what I should check, then I’ll be happy to run some experiment and report the result.

Источник

Оцените статью