....
Dim VERIFICAPASSWORD As HttpJob
VERIFICAPASSWORD.Initialize("", Me)
VERIFICAPASSWORD.Download2(linkwebsitefilephp, Array As String(......))
Wait For (VERIFICAPASSWORD) JobDone(VERIFICAPASSWORD As HttpJob)
If VERIFICAPASSWORD.Success Then
Dim res As String
res = VERIFICAPASSWORD.GetString
Log("LastRow ID: " & res)
Else
Log("Error: " & VERIFICAPASSWORD.ErrorMessage)
End If
VERIFICAPASSWORD.Release
....
sql.insert_book=INSERT INTO books (title) VALUES (?);
sql.select_last_id=SELECT IFNULL(LAST_INSERT_ID(), 0) AS id;
B4A client
B4X:
Sub Activity_Create(FirstTime As Boolean)
AddBook("Test")
End Sub
Sub AddBook (Name As String)
Dim cmd As DBCommand = CreateCommand("insert_book", Array(Name))
Dim j As HttpJob = CreateRequest.ExecuteBatch(Array(cmd), Null)
Wait For(j) JobDone(j As HttpJob)
If j.Success Then
Log("Inserted successfully!")
End If
j.Release
GetLastId
End Sub
Sub GetLastId
Dim req As DBRequestManager = CreateRequest
Dim cmd As DBCommand = CreateCommand("select_last_id", Null)
Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
If j.Success Then
req.HandleJobAsync(j, "req")
Wait For (req) req_Result(res As DBResult)
'work with result
'req.PrintTable(res)
' just get first row and first column
'Dim row() As Object = res.Rows.Get(0)
'Log(row(0))
' or loop for any row
For Each row() As Object In res.Rows
Log(row(res.Columns.Get("id")))
Next
Else
Log("ERROR: " & j.ErrorMessage)
End If
j.Release
End Sub
Hi: Searching several threads in the forum, I've used this code to add a row in a "master" table, and then the "children rows" in a "detail" table. May be useful to someone. Improvements are welcome. Table Master: id (primary, autoincrement), field 1, field 2... Table Detail: id, master_id...
What about a stored procedure that executes one after the other the INSERT statement and the SELECT for last_insert_id() ?
Same problem when multiple clients insert data at the same time or we can count on an internal locking mechanism that assures each one will receive the correct last_id?
JDBC can actually return the values of auto-increment keys. I've done a test modification of jRDC2, but I just have not published it yet (it requires a modified DBRequestManager). I'll post my modifications in another thread and post a link here. Any questions/issues with the modification can then be addressed in the new thread.
Would you be guaranteed to get the right id with the code below because it's the same connection?
I'm fairly certain the answer is yes but I'm using mysql for the first time.
B4X:
Dim sql1 As SQL = Main.pool.GetConnection
sql1.ExecNonQuery2("INSERT INTO etc", Array As Object(etc))
Dim lastID As Int = sql1.ExecQuerySingleResult("SELECT LAST_INSERT_ID()")
sql1.Close