0

When I add this code to my Android 2.1 Java app, it fails:

db=SQLiteDatabase.openOrCreateDatabase("Locations", null);

Do I need a special permission to create a database, or can anybody provide a suggestion how to find out what goes wrong?

Here is the LogCat output:

04-04 15:04:28.702: DEBUG/ddm-heap(703): Got feature list request
04-04 15:04:29.642: ERROR/Database(703): sqlite3_open_v2("Locations", &handle, 6, NULL) failed
04-04 15:04:29.652: DEBUG/AndroidRuntime(703): Shutting down VM
04-04 15:04:29.662: WARN/dalvikvm(703): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-04 15:04:29.662: ERROR/AndroidRuntime(703): Uncaught handler: thread main exiting due to uncaught exception
04-04 15:04:29.702: ERROR/AndroidRuntime(703): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gormtech.zyx.RejseplanQuick/com.xxx.zyx.xxx.HelloAndroid}: android.database.sqlite.SQLiteException: unable to open database file
3
  • 1
    Could you be more specific about "it fails", such as posting the Java stack trace? You can access that stack trace via adb logcat, DDMS, or the DDMS perspective in Eclipse. FWIW, I really recommend you use SQLiteOpenHelper rather than manually open the database the way you are, so that you can get assistance for updating your schema when it changes. Commented Apr 4, 2010 at 12:21
  • It is very important for me to understand how things work before I start using obscure methods like SQLiteOpenHelper. Otherwise I could easily end up with an app that is unstable or slow, without having any idea why. Commented Apr 4, 2010 at 13:02
  • I have added logcat output. The error message is that it cannot open the database file, and the GUI behavior is a Force Close. Commented Apr 4, 2010 at 13:09

1 Answer 1

2

Any app can make a SQLiteDatabase without any special permissions.

I'm a little thrown by your code snippet because in my documents/SDK openOrCreateDatabase (on Context) takes three parameters: name, operating mode and factory. https://developer.android.com/reference/android/content/Context.html

It will be very revealing to see the output of your 'adb logcat'. Either run the adb application from a console, or bring up the LogCat window in Eclipse (it might help to switch to the DDMS Perspective). Run the app, get the failure and paste the later lines here.

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

4 Comments

I am not using the Context class, but the SQLiteDatabase class, where it only takes 2 parameters.
That may well be the source of your difficulty. From a quick look at the source it seems that 'path' parameter taken by the SQLiteDatabase version of openOrCreateDatabase is the 'The full path to the database'. The context version however appears to convert the name to a full path with getDatabasePath(). google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/… It feeds through to the constructor: google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/…
It seems to be documented here as a bug: code.google.com/p/android/issues/detail?id=949
This solved it: db=this.openOrCreateDatabase("Locations.db", MODE_PRIVATE, null); - thanks

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.