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:
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?
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
con.ExecNonQuery2

RBS
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
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.
B4X:
Dim MaxRec As Int 'or long
MaxRec=sql.ExecQuerySingleResult("SELECT last_insert_rowid() FROM mytable" )
Log($"Last rec: ${MaxRec}"$)
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
ok thought i could use the output clause to minimize my queries
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
I had tried that the other day too, but then switched to last_insert_rowid.
B4X:
    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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…