Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>A few things:</strong></p> <ol> <li><a href="https://stackoverflow.com/questions/8752193/fastest-and-most-efficient-way-to-pre-populate-database-in-android/8855122#8855122">See my answer here</a> for general tips when doing bulk INSERTs.</li> <li>There is no need to have a temporary container for your INSERT statements (in this case, the ArrayList&lt;>). Simply use <a href="https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#beginTransaction%28%29" rel="nofollow noreferrer">beginTransaction()</a> and <a href="https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#endTransaction%28%29" rel="nofollow noreferrer">endTransaction()</a> in a try...finally.</li> <li>Utilize pre-compiled statements via <a href="https://developer.android.com/reference/android/database/sqlite/SQLiteStatement.html" rel="nofollow noreferrer">SQLiteStatement</a> vs building each INSERT statement as in your example. This is needless thrashing.</li> </ol> <p><strong>Quick and dirty example:</strong></p> <pre><code>// note: untested code used for illustration! private boolean bulkInsertData(SQLiteDatabase db, final String tableName) { final int NUM_ROWS = 10000000; Random random = new Random(); try { SQLiteStatement insStmt = insStmt = db.compileStatement("INSERT INTO " + tableName + " (a, b, c) VALUES (?, ?, ?);"); db.beginTransaction(); try { for(int i = 0; i &lt; NUM_ROWS; i++) { insStmt.bindLong(1, i); insStmt.bindLong(2, random.nextInt(100000)); insStmt.bindString(3, String.valueOf(i)); insStmt.executeInsert(); // should really check value here! } db.setTransactionSuccessful(); } finally { db.endTransaction(); } } catch(SQLException se) { return false; } return true; } </code></pre>
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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