3

I am having a problem inserting multiple entries into an Android SQLite database. I insert like so with db.execQuery():

String insertString = "INSERT INTO coordinates 
    SELECT 0 AS _id, 5 AS associated_route_id, 38.88945 AS latidude, -77.034821 AS longitude 
    UNION SELECT 1, 5, 38.889671, -77.034912 
    UNION SELECT 2, 5, 38.890041, -77.035316"
database.execSQL(insertString);

I pull later with the same db like so:

String[] columns = new String[] { "latitude", "longitude" };
Cursor cursor = db.query("coordinates", columns,
                "associated_route_id=5", null, null, null,
                null);

cursor.moveToFirst();
if (cursor.getCount() == 0)
// No rows in cursor

I got this to work using db.insert(table, null, contentValues), but replaced the insert to make things faster.

The problem is that the cursor is empty which makes it seem like the insert is not working. Why doesn't it work?

3
  • 1
    what is your question? Commented Apr 23, 2013 at 17:33
  • The cursor returns nothing. Commented Apr 23, 2013 at 17:36
  • Can you post your insert code? Are you using sql script or inserting with content value? Commented Apr 23, 2013 at 17:37

2 Answers 2

10

Answering my own question.

I used a different approach to insert the entries. I used Trinimon's suggestion to use db.compileStatement, but what increased insertion time the most was adding:

db.startTransaction();
//insert lots of stuff...
database.setTransactionSuccessful();
database.endTransaction();

Inserting 500 entries decreased from 45 seconds to 300 milliseconds.

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

2 Comments

Thought about proposing this, but hadn't assumed that it would be such a performance booster. Usually you can use executeBatch() with JDBC what is also a quite nice feature (viralpatel.net/blogs/batch-insert-in-java-jdbc).
Thanks, that answer really helped me! It's worth noting that Trinimon's example for syntax that could be compiled is not valid, the correct example should be: String sql = "INSERT INTO coordiantes VALUES(?, ?, ?, ?)"
2

The fastest way to insert records is to use prepared statements with bind variables, because the statement doesn't need to be compiled all the time, e.g:

String sql = "INSERT INTO coordinates (?, ?, ?, ?)";
SQLiteStatement statement = db.compileStatement(sql);

// loop through records or arrays and assign values
for (...) { 
    long id = ...
    long associated_route_id = ...
    double latidude = ...
    double longitude = ...

    statement.bindLong  (1, id);
    statement.bindLong  (2, associated_route_id);
    statement.bindDouble(3, latidude);
    statement.bindDouble(4, longitude);

    statement .execute();
}

Hope this helps ... Cheers!

2 Comments

Oops, copy/paste error. I took out some fields to simplify the problem. I updated the question. I will try and implement this insert method to see if it works. Thank you.
Ok, I removed my hint from here 'cause it's no longer valid.

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.