Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>NOTE: This answer is outdated. If you are using Roboletric 2.X, please see <a href="https://stackoverflow.com/a/24578332/850787">https://stackoverflow.com/a/24578332/850787</a></h2> <p>The problem is that Robolectric's SQLiteDatabase is stored only in memory, so when you call getReadableDatabase or getWritableDatabase, the existing database will be overridden with new empty database. </p> <p>I was running to the same problem and only solution I found was that I needed to fork the Robolectric project and added ShadowSQLiteOpenHelper to save database if same context is given two times. However the problem with my fork is that I had to 'disable' close()-function when contex is given because otherwise Connection.close() will destroy the database in memory. I have made pull request for it but it isn't merged to project yet.</p> <p>But feel free to clone my version and it should fix your problem (If I understood it correctly :P ). It can be found on GitHub: <a href="https://github.com/waltsu/robolectric" rel="nofollow noreferrer">https://github.com/waltsu/robolectric</a></p> <p>Here is a example how to use the modification:</p> <pre><code>Context c = new Activity(); SQLiteOpenHelper helper = new SQLiteOpenHelper(c, "path", null, 1); SQLiteDatabase db = helper.getWritableDatabase(); // With the db write something to the database db.query(...); SQLiteOpenHelper helper2 = new SQLiteOpenHelper(c, "path", null, 1); SQLitedatabase db2 = helper2.getWritableDatabase(); // Now db and db2 is actually the same instance Cursor c = db2.query(...) ; // Fetch the data which was saved before </code></pre> <p>Ofcourse you don't need to create new SQLiteOpenHelper, but that is just example that passing same context to two different SQLiteOpenHelper will give same database.</p>
    singulars
    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