The new resumable subs feature, introduced in B4J v5.50, B4i v4.00 and B4A v7.00 (to be soon released), makes it simpler to work with large data sets with minimum effect on the program responsiveness.
The new standard way to insert data is:
The steps are:
- Call AddNonQueryToBatch for each commands that should be issued.
- Execute the commands with ExecNonQueryBatch. This is an asynchronous method. The commands will be executed in the background and the NonQueryComplete event will be raised when done.
- This call returns an object that can be used as the sender filter parameter. This is important as there could be multiple background batch executions running. With the filter parameter the event will be caught by the correct Wait For call in all cases.
- Note that SQL.ExecNonQueryBatch begins and ends a transaction internally.
Queries
In most cases the queries will be fast and should therefore be issued synchronously with SQL.ExecQuery2. However if there is a slow query then you should switch to SQL.ExecQueryAsync:
As in the previous case, the ExecQueryAsync method returns an object that is used as the sender filter parameter.
Tips:
1. ResultSet type in B4A extends the Cursor type. You can change it to Cursor if you prefer. The advantage of using ResultSet is that it is compatible with B4J and B4i.
2. If the number of rows returned from the query is large then the Do While loop will be slow in debug mode. You can make it faster by putting it in a different sub and cleaning the project (Ctrl + P):
This is related to a debugger optimization that is currently disabled in resumable subs.
The performance of both solutions will be the same in release mode.
B4J
- Requires jSQL v1.50+ (https://www.b4x.com/android/forum/threads/updates-to-internal-libaries.48274/#post-503552).
- Recommended to set the journal mode to WAL: https://www.b4x.com/android/forum/t...ent-access-to-sqlite-databases.39904/#content
The new standard way to insert data is:
B4X:
For i = 1 To 1000
sql.AddNonQueryToBatch("INSERT INTO table1 VALUES (?)", Array(Rnd(0, 100000)))
Next
Dim SenderFilter As Object = sql.ExecNonQueryBatch("SQL")
Wait For (SenderFilter) SQL_NonQueryComplete (Success As Boolean)
Log("NonQuery: " & Success)
The steps are:
- Call AddNonQueryToBatch for each commands that should be issued.
- Execute the commands with ExecNonQueryBatch. This is an asynchronous method. The commands will be executed in the background and the NonQueryComplete event will be raised when done.
- This call returns an object that can be used as the sender filter parameter. This is important as there could be multiple background batch executions running. With the filter parameter the event will be caught by the correct Wait For call in all cases.
- Note that SQL.ExecNonQueryBatch begins and ends a transaction internally.
Queries
In most cases the queries will be fast and should therefore be issued synchronously with SQL.ExecQuery2. However if there is a slow query then you should switch to SQL.ExecQueryAsync:
B4X:
Dim SenderFilter As Object = sql.ExecQueryAsync("SQL", "SELECT * FROM table1", Null)
Wait For (SenderFilter) SQL_QueryComplete (Success As Boolean, rs As ResultSet)
If Success Then
Do While rs.NextRow
Log(rs.GetInt2(0))
Loop
rs.Close
Else
Log(LastException)
End If
Tips:
1. ResultSet type in B4A extends the Cursor type. You can change it to Cursor if you prefer. The advantage of using ResultSet is that it is compatible with B4J and B4i.
2. If the number of rows returned from the query is large then the Do While loop will be slow in debug mode. You can make it faster by putting it in a different sub and cleaning the project (Ctrl + P):
B4X:
Wait For (SenderFilter) SQL_QueryComplete (Success As Boolean, rs As ResultSet)
If Success Then
WorkWithResultSet(rs)
Else
Log(LastException)
End If
End Sub
Sub WorkWithResultSet(rs As ResultSet)
Do While rs.NextRow
Log(rs.GetInt2(0))
Loop
rs.Close
End Sub
The performance of both solutions will be the same in release mode.
B4J
- Requires jSQL v1.50+ (https://www.b4x.com/android/forum/threads/updates-to-internal-libaries.48274/#post-503552).
- Recommended to set the journal mode to WAL: https://www.b4x.com/android/forum/t...ent-access-to-sqlite-databases.39904/#content