I am trying to create a database on my sd card. Whenever I call SQLiteDatabase.openOrCreateDatabase I get the error:
07-21 13:33:17.587: ERROR/AndroidRuntime(5541): Caused by: android.database.sqlite.SQLiteException: unable to open database file
Does anyone know what may be causing this? Here is the code I have in the open() method of my database class:
File sdcard = Environment.getExternalStorageDirectory();
String dbfile = sdcard.getAbsolutePath() + File.separator+ "external_sd" + File.separator + Schema.DATABASE_NAME ;
db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
Creating And Updating Database In Android It provides two methods onCreate(SQLiteDatabase db), onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion). The SQLiteOpenHelper is responsible for opening database if exist, creating database if it does not exists and upgrading if required.
SQLite Database is an open-source database provided in Android which is used to store data inside the user's device in the form of a Text file. We can perform so many operations on this data such as adding new data, updating, reading, and deleting this data.
Android SQLite is a very lightweight database which comes with Android OS. Android SQLite combines a clean SQL interface with a very small memory footprint and decent speed. For Android, SQLite is “baked into” the Android runtime, so every Android application can create its own SQLite databases.
One possible fault on this could be that you did not set the appropriate permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
However storing data on a SD Card is considered to be insecure. Because every application which has permissions to store data on the sd storage could access the database.
Edit: Another possible problem is that external storage may not be available. You need to make sure that your external storage is currently writeable. One method would be to determine the state via getExternalStorageState
(see reference here).
getExternalStorageState() returns MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage.
Reference. There is also a stackoverflow post on checking the state here: Writing Text File to SD Card fails
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With