Android sqlite with multi thread

20,260

Solution 1

Number 1 is the answer. I have several stack answers and blog posts about how exactly to do that:

What are the best practices for SQLite on Android?

http://touchlabblog.tumblr.com/post/24474750219/single-sqlite-connection

Number 2 is a lot of extra work and unnecessary. ContentProvider exists so you can share data with other apps. Its used to manage database connections because people don't understand how Sqlite and Android work together.

Solution 2

I suggest you use a ContentProvider with a LoaderManager whenever you can. It is capable of performing queries in the background automatically.

Share:
20,260
mobile app Beginner
Author by

mobile app Beginner

Updated on February 17, 2020

Comments

  • mobile app Beginner
    mobile app Beginner over 4 years

    I am writing a android application using sqlite. There are many activities and one service. I use the DB from more than one thread. It works perfectly in Android 2.X, but once I run it in Android 3.X it always throws this error and Force Close:

    05-04 22:17:04.815: I/SqliteDatabaseCpp(8774): sqlite returned: error code = 5, msg = database is locked, db=/data/data/xxx/databases/im
    05-04 22:17:04.815: E/SqliteDatabaseCpp(8774): sqlite3_open_v2("/data/data/xxx/databases/im", &handle, 6, NULL) failed
    05-04 22:17:04.835: E/SQLiteDatabase(8774): Failed to open the database. closing it.
    05-04 22:17:04.835: E/SQLiteDatabase(8774): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
    05-04 22:17:04.835: E/SQLiteDatabase(8774):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
    05-04 22:17:04.835: E/SQLiteDatabase(8774):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
    05-04 22:17:04.835: E/SQLiteDatabase(8774):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
    05-04 22:17:04.835: E/SQLiteDatabase(8774):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
    05-04 22:17:04.835: E/SQLiteDatabase(8774):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:790)
    05-04 22:17:04.835: E/SQLiteDatabase(8774):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
    05-04 22:17:04.835: E/SQLiteDatabase(8774):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
    

    Anyone knows why it happen and how to solve it?

    I have researched in the internet and most people advise:

    1. Use one DB connection only for the application. How to ensure it? I want to share the DB connection for both of the Service and Activities. Should I do it by create a public static DB variable?
    2. ContentProvider - I am using complicated SQL Statement in code (Such as joining few tables, temporary table). Is it possible to run these complicated SQL statement in ContentProvider?


      Thank you everyone. Finally, (1) works fine for me. But I still wonder why Android 2.X does not have this problem.
  • mobile app Beginner
    mobile app Beginner about 12 years
    Thanks for advice, but it requires many extra works and the database will not share with other applications. I think solution (1) is better for me. I will think about your suggested solution if (1) does work. Thanks
  • mobile app Beginner
    mobile app Beginner about 12 years
    Thanks Kevin Galligan, I have read your posts. I learned a lot at - stackoverflow.com/questions/2493331/…. I am trying solution (1). I will study ormlite if solution (1) fail. Thank you!
  • Kevin Galligan
    Kevin Galligan about 12 years
    ORMLite is nice, but totally not necessary for database access from multiple threads. Just have a static SQLiteOpenHelper instance, or keep one instance in an Application instance. You'll never have locked db issues if you only have one instance.
  • Kevin Galligan
    Kevin Galligan about 12 years
    I know a lot of people that prefer ContentProvider, but its totally not necessary for multi-threaded db access. If you're only using it for that, its not needed. However, if you like ContentProvider, use it. Just seems like extra work to me, though.
  • mobile app Beginner
    mobile app Beginner about 12 years
    Thanks Kevin, ORMLite is awesome anyway