Since this is a very rich conference I'm watching it once again, and at minute 24:17 I noticed Virgil said
Also, use transactions when you use SQLite, not only will they preserve the data integrity, but they will increase the performance of your database operations
EDIT: What exactly does he means with "use transactions", does he means to tell us to use BEGIN TRANSACTION statement, or is he referred to something else?
If the first one is it then:
does that mean we should use
SQLiteDatabase#rawQuery()method to write raw SQL statements instead of the providedSQLiteDatabase#query()method?what is the difference between it and using a
SELECTstatement and aTRANSACTIONstatement?
Simple example to explain you the need for database transactions and use of prepared statements etc.
While inserting large number of records i.e thousands of records or so, we run into the problem of “insert speed”. The usual insert command in Android is slow, so we can use a transaction and prepared statement.
In our case, we use INSERT OR REPLACE INTO on the insert query since we want to update a row if it already exists, based on the trigger (INDEX) created.
If you’re using INSERT OR REPLACE INTO command, you have to create a trigger. This SQL trigger is executed after the table has been created (see DatabaseHandler.java below)
Another important factor in speeding up your insert is using prepared statements.
You can find the example below :
MainActivity.java – contains the AsyncTask that will be executed to insert larger number of data to db when the user clicks a button.
}
DatabaseHandler.java – handles the database operations such as table creation, emptying the database, counting database records and the inserting data using a loop.
}
activity_main.xml – the layout so we can enter the desired number of records to be inserted, choose whether we want it to be a ‘normal’ or ‘fast’ insert, and the status of the operation.