The current AddNonQueryToBatch/ExecNonQueryBatch is very flexible by allowing various non-query statements (update, instert, delete, etc.) to be batched together. But often, it is just one type of non-query statement that needs to be executed multiple times. Even the example given for AddNonQueryToBatch is such a case:
Even though it is the same statement for 1000 iterations, the underlying library code (https://github.com/AnywhereSoftware/B4J_SQL/blob/master/src/anywheresoftware/b4j/objects/SQL.java), adds the arguments of each AddNonQueryToBatch to a list (with the above code creating a list of 1000 entries). The ExecNonQueryBatch then iterates over that list (https://github.com/AnywhereSoftware...rc/anywheresoftware/b4j/objects/SQL.java#L251) and calls another method. That method creates a prepared statement, sets any parameters, and executes the prepared statement. So for the above loop, 1000 prepared statements are created and executed.
With the three new methods, the following can be accomplished.
SetNonQuery2Statment - this methods takes a non-query SQL statement (parameterized or not) and creates a prepared statement. (Edit: non-parameterized query statements probably don't make any sense with this)
SetNonQuery2Args - this method can be called multiple times to set the arguments for each call of the prepared statement with SetNonQuery2Statement
ExecNonQuery2Batch - this methods loops over the list of arguments and calls the prepared statements execute method with each set of arguments.
The above code would then be
Technically this could be implemented with JavaObject(s), but it would be cool to have it part of the SQL library (and therefore I'm placing this wish).
Good grief, how do you edit the title? (found it!)
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)
Even though it is the same statement for 1000 iterations, the underlying library code (https://github.com/AnywhereSoftware/B4J_SQL/blob/master/src/anywheresoftware/b4j/objects/SQL.java), adds the arguments of each AddNonQueryToBatch to a list (with the above code creating a list of 1000 entries). The ExecNonQueryBatch then iterates over that list (https://github.com/AnywhereSoftware...rc/anywheresoftware/b4j/objects/SQL.java#L251) and calls another method. That method creates a prepared statement, sets any parameters, and executes the prepared statement. So for the above loop, 1000 prepared statements are created and executed.
With the three new methods, the following can be accomplished.
SetNonQuery2Statment - this methods takes a non-query SQL statement (parameterized or not) and creates a prepared statement. (Edit: non-parameterized query statements probably don't make any sense with this)
SetNonQuery2Args - this method can be called multiple times to set the arguments for each call of the prepared statement with SetNonQuery2Statement
ExecNonQuery2Batch - this methods loops over the list of arguments and calls the prepared statements execute method with each set of arguments.
The above code would then be
B4X:
sql.SetNonQuery2Statement("INSERT INTO table1 VALUES (?)")
For i = 1 To 1000
sql.AddNonQuery2Args(Array(Rnd(0, 100000)))
Next
Dim SenderFilter As Object = sql.ExecNonQuery2Batch("SQL")
Wait For (SenderFilter) SQL_NonQueryComplete (Success As Boolean)
Log("NonQuery: " & Success)
Technically this could be implemented with JavaObject(s), but it would be cool to have it part of the SQL library (and therefore I'm placing this wish).
Good grief, how do you edit the title? (found it!)
Last edited: