SQL: Select LAST_INSERT_ID();
-
yannickHas successfully completed the online course Advanced (300)5 years ago #14719
Hi,
I would like to execute an insert and receive the ID of the insert dataset.
sample query:
INSERT INTO tbl(description) VALUES('rec1'), VALUES('rec2'); SELECT LAST_INSERT_ID();
using default mysql settings, this query should return 2.
According to your documentation -> https://community.simplifier.io/doc/current-release/#8319:
Inserts are just possible using the mode execute,
but the execute mode doesn’t return any result.2 Connector Calls in a row doesn’t work, the result of the SELECT LAST_INSERT_ID() querry always returns 0!
INFO: For the Select LAST_INSERT_ID() query it is necessary that both statements are executed using the same connection ID.
How do i do that?
Chris BouveretHas successfully completed the online course IntroductionHas successfully completed the online course Intermediate (200)Has successfully completed the online course Advanced (300)Has successfully completed the online course Basics (100)Has successfully completed the online course Advanced (320)Has successfully completed the Intermediate CertificationHas successfully completed the Advanced Certification5 years ago #15510::The best way would be to create a business object and a method for your insert. You can then go ahead and call your SQL Connector. Just du something like that in your method:
try{ var connectorCallInsert = Simplifier.Connector.MySQLConnector.MySQLcall({"mode": "execute","request" : "Your Insert query"}); var connectorCallLastId = Simplifier.Connector.MySQLConnector.MySQLcall({"mode": "query","request" : "SELECT LAST_INSERT_ID();"}); output.lastId = connectorCallLastId; output.success = true; } catch (e) { output.message = e.message; output.success = false; }
Make sure to add the output parameters “lastId”,”success” and “message” in your Business Object Method
The above BusinessObject executes your insert first and then gets the last Id inserted as outputHope that helps
Chris BouveretHas successfully completed the online course IntroductionHas successfully completed the online course Intermediate (200)Has successfully completed the online course Advanced (300)Has successfully completed the online course Basics (100)Has successfully completed the online course Advanced (320)Has successfully completed the Intermediate CertificationHas successfully completed the Advanced CertificationKahnHas successfully completed the online course IntroductionHas successfully completed the online course Intermediate (200)Has successfully completed the online course Advanced (300)anuragkumar5 years ago #15567::Hi Team, @chris,
Will the above code snippet with below command apply for Sqlite db as well:
SELECT LAST_INSERT_ID();Because I have a connector created with below inputs:
“mode”:”query”,
“request”:”SELECT LAST_INSERT_ID()”;But it give below error message;
{
“message”: “[SQLITE_ERROR] SQL error or missing database (no such function: LAST_INSERT_ID)”,
“success”: false
}Please have a check and kindly suggest.
Regards
AnuragChris BouveretHas successfully completed the online course IntroductionHas successfully completed the online course Intermediate (200)Has successfully completed the online course Advanced (300)Has successfully completed the online course Basics (100)Has successfully completed the online course Advanced (320)Has successfully completed the Intermediate CertificationHas successfully completed the Advanced CertificationMathieu RollHas successfully completed the online course IntroductionHas successfully completed the online course Intermediate (200)Has successfully completed the online course Advanced (300)Has successfully completed the online course Basics (100)5 years ago #15624::Hi,
the problem is that Simplifier is generating a new session each time you call the connector.
so you can’t use the last inserted methods because they are pointing on the session wich is a new one and so you will always get 0 as last inserted id.
with the method chris gave you he is always reading the highest number in the datebase so its not session based.
so yes if an other user inserts something at the same time you will get his id.
a better way to get the id is by using the max function. this will save some performance.
SELECT MAX(id) AS id FROM test;
but same here you will always get the latest id from any user.
You must be logged in to reply to this topic.