Android Question [SQL] OUTPUT clause of the INSERT statement

Blueforcer

Well-Known Member
Licensed User
Longtime User
I need a return value for an INSTERT query, to get the primary key with Auto-increment for this query

B4X: Copy
strSQL = $"INSERT INTO tbl_sessions (game, note, time,user,friends) OUTPUT Inserted.session_id VALUES (?,?,?,?,?)"$
result = con.ExecQuery2(strSQL, Array As Object(game,note,time,userID,jp.ToString))

But i get an error

java.sql.SQLException: Can not issue data manipulation statements with executeQuery().

how to solve it?
 
I need a return value for an INSTERT query, to get the primary key with Auto-increment for this query

B4X: Copy
strSQL = $"INSERT INTO tbl_sessions (game, note, time,user,friends) OUTPUT Inserted.session_id VALUES (?,?,?,?,?)"$
result = con.ExecQuery2(strSQL, Array As Object(game,note,time,userID,jp.ToString))

But i get an error



how to solve it?
con.ExecNonQuery2

RBS
 
Upvote 0
ExecNonQuery2 doesnt return anything
Yes, it does not return anything. You have to run the below code immeditely after the insert to get the rowid.
Code: Copy
Dim MaxRec As Int 'or long
MaxRec=sql.ExecQuerySingleResult("SELECT last_insert_rowid() FROM mytable" )
Log($"Last rec: ${MaxRec}"$)
 
Upvote 0
Yes, it does not return anything. You have to run the below code immeditely after the insert to get the rowid.
Code: Copy
Dim MaxRec As Int 'or long
MaxRec=sql.ExecQuerySingleResult("SELECT last_insert_rowid() FROM mytable" )
Log($"Last rec: ${MaxRec}"$)
ok thought i could use the output clause to minimize my queries
 
Upvote 0
I had tried that the other day too, but then switched to last_insert_rowid.
B4X: Copy
    Dim DR As ResultSet = m_SQL.ExecQuery("SELECT last_insert_rowid() AS Appointments_Id;")
    Do While DR.NextRow
        Appointment.Id = DR.GetInt("Appointments_Id")
    Loop
    DR.Close
 
Upvote 0
Back
Top