Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Inserts, updates, deletes and reads are generally OK from multiple threads, but Brad's <a href="https://stackoverflow.com/a/2493839/2759862">answer</a> is not correct. You have to be careful with how you create your connections and use them. There are situations where your update calls will fail, even if your database doesn't get corrupted.</p> <p><strong>The basic answer.</strong></p> <p>The SqliteOpenHelper object holds on to one database connection. It appears to offer you a read and write connection, but it really doesn't. Call the read-only, and you'll get the write database connection regardless.</p> <p>So, one helper instance, one db connection. Even if you use it from multiple threads, one connection at a time. The SqliteDatabase object uses java locks to keep access serialized. So, if 100 threads have one db instance, calls to the actual on-disk database are serialized.</p> <p>So, one helper, one db connection, which is serialized in java code. One thread, 1000 threads, if you use one helper instance shared between them, all of your db access code is serial. And life is good (ish).</p> <p>If you try to write to the database from actual distinct connections at the same time, one will fail. It will not wait till the first is done and then write. It will simply not write your change. Worse, if you don’t call the right version of insert/update on the SQLiteDatabase, you won’t get an exception. You’ll just get a message in your LogCat, and that will be it.</p> <p>So, multiple threads? Use one helper. Period. If you KNOW only one thread will be writing, you MAY be able to use multiple connections, and your reads will be faster, but buyer beware. I haven't tested that much.</p> <p>Here's a blog post with far more detail and an example app.</p> <ul> <li><a href="http://touchlabblog.tumblr.com/post/24474398246/android-sqlite-locking" rel="noreferrer">Android Sqlite Locking</a> (Updated link 6/18/2012)</li> <li><a href="https://github.com/touchlab/Android-Database-Locking-Collisions-Example" rel="noreferrer">Android-Database-Locking-Collisions-Example by touchlab</a> on GitHub</li> </ul> <p>Gray and I are actually wrapping up an ORM tool, based off of his Ormlite, that works natively with Android database implementations, and follows the safe creation/calling structure I describe in the blog post. That should be out very soon. Take a look.</p> <hr> <p>In the meantime, there is a follow up blog post:</p> <ul> <li><a href="http://touchlabblog.tumblr.com/post/24474750219/single-sqlite-connection" rel="noreferrer">Single SQLite connection</a></li> </ul> <p>Also checkout the fork by <em>2point0</em> of the previously mentioned locking example:</p> <ul> <li><a href="https://github.com/2point0/Android-Database-Locking-Collisions-Example" rel="noreferrer">Android-Database-Locking-Collisions-Example by 2point0</a> on GitHub</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload