SQLiteStatement does SELECT / INSERT / DELETE / UPDATE

I use compiled SQLiteStatement with transactions in SQLite transaction optimization, but I read the documentation for the execute function

Execute this SQL statement if it is not SELECT / INSERT / DELETE / UPDATE, e.g. CREATE / DROP table, view, trigger, index, etc.

This means that this function should not be used with SELECT / INSERT / DELETE / UPDATE , but I have code that uses it with insertion and works.

I know executeInsert and other methods, but executeUpdateDelete not available at my API level, so can I use execute ?

Also, if I don't need the last insert identifier or the number of rows affected, should I use execute instead of executeInsert , etc., in other words, is this more efficient?

+6
android sqlite android-sqlite
Nov 13 '12 at 17:23
source share
2 answers

execute , probably no faster than executeInsert , can even be slower (on ICS, execute calls executeUpdateDelete and discards the return value). You need to check this out, but I doubt you will find the real difference here.

AFAIK. Itโ€™s safe to use only execute if you donโ€™t need the return values, but I wonโ€™t expect this to be true in future versions of Android. The documentation says no, so maybe someone will change the behavior to reflect this. Old implementations also seem to use execute (e.g. 2.1 delete() source code ). Jelly Bean, for example, has changed a lot behind the scenes of SQLite, but it should still work when using execute

Also, if you are not using the same SQLiteStatement again and again, just SQLiteStatement through the arguments, this is probably not worth using. Building a new one with every regular call to insert , update , ... quickly compares with the actual access to the database and the required disk I / O. On the other hand, transactions are very helpful, because synchronizing the state of the database on disk for each operator is very slow.

+3
Nov 13
source share

Use SQLiteDatabase instead of SQLiteStatement to interact with your database. SQLiteStatements are not thread safe, so I would not use them for SELECT / INSERT / DELETE / UPDATE. You should also try not to use raw database queries for anything other than Selects . There are built-in helper functions that will improve the performance of your database. In your instance of SQLiteDatabase , you have .insert, .update, .delete and I use .rawQuery for Select.

+1
Nov 13 '12 at 18:08
source share



All Articles