Blueforcer Well-Known Member Licensed User Longtime User May 13, 2022 #1 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(). Click to expand... 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: 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(). Click to expand... how to solve it?
R RB Smissaert Well-Known Member Licensed User Longtime User May 13, 2022 #2 Blueforcer said: 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 how to solve it? Click to expand... con.ExecNonQuery2 RBS Upvote 0
Blueforcer said: 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 how to solve it? Click to expand... con.ExecNonQuery2 RBS
Blueforcer Well-Known Member Licensed User Longtime User May 13, 2022 #3 RB Smissaert said: con.ExecNonQuery2 RBS Click to expand... ExecNonQuery2 doesnt return anything Upvote 0
M Mahares Expert Licensed User Longtime User May 13, 2022 #4 Blueforcer said: ExecNonQuery2 doesnt return anything Click to expand... 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 said: ExecNonQuery2 doesnt return anything Click to expand... 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}"$)
Blueforcer Well-Known Member Licensed User Longtime User May 13, 2022 #5 Mahares said: 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}"$) Click to expand... ok thought i could use the output clause to minimize my queries OUTPUT clause (Transact-SQL) - SQL Server Returns information from, or expressions based on, each row affected by an INSERT, UPDATE, DELETE, or MERGE statement. docs.microsoft.com Upvote 0
Mahares said: 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}"$) Click to expand... ok thought i could use the output clause to minimize my queries OUTPUT clause (Transact-SQL) - SQL Server Returns information from, or expressions based on, each row affected by an INSERT, UPDATE, DELETE, or MERGE statement. docs.microsoft.com
Alexander Stolte Expert Licensed User Longtime User May 13, 2022 #6 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
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
Blueforcer Well-Known Member Licensed User Longtime User May 13, 2022 #7 Yep i also do this now. btw for mysql it is : Dim SessionID As Int = con.ExecQuerySingleResult("SELECT LAST_INSERT_ID() FROM tbl_sessions" ) Click to expand... Upvote 0
Yep i also do this now. btw for mysql it is : Dim SessionID As Int = con.ExecQuerySingleResult("SELECT LAST_INSERT_ID() FROM tbl_sessions" ) Click to expand...