CocaColaBear Posted October 2, 2013 Share Posted October 2, 2013 Hi all! This example: addEventHandler ( "onPlayerSpawn", root, function() dbExec(bd, "INSERT INTO TABLE1 ...", ...) -- auto_increment id dbExec(bd, "INSERT INTO TABLE2 VALUES(?) ...", autoincrement_id_from_TABLE1) end ) How return latest autoincrement id? If 10 users spawns then Last_insert_id() returns incorrent value, or not? This is just an example of several queries at once, I do not use onPlayerSpawn: D I use MySQL. Link to comment
Discord Moderators Zango Posted October 2, 2013 Discord Moderators Share Posted October 2, 2013 Good question! Using MTA's dbQuery I would insert the values into your first table, and retrieve the value of MySQL LAST_INSERT_ID. Example: local bExec = dbExec (connection, 'INSERT INTO TABLE1 ...') if (bExec) then local queryData = dbQuery (connection, 'SELECT LAST_INSERT_ID()') local result = dbPoll (queryData, -1) local autocrement_id = result[1]['LAST_INSERT_ID()'] end You can then reference last_insert_id in your next insert queries. Happy scripting Link to comment
myonlake Posted October 7, 2013 Share Posted October 7, 2013 Since MTA:SA build r5862 you are able to do it without doing any queries after the initial query. Here is the Wiki example. local result, num_affected_rows, last_insert_id = dbPoll ( qh, -1 ) if result == nil then outputConsole( "dbPoll result not ready yet" ) elseif result == false then local error_code,error_msg = num_affected_rows,last_insert_id outputConsole( "dbPoll failed. Error code: " .. tostring(error_code) .. " Error message: " .. tostring(error_msg) ) else outputConsole( "dbPoll succeeded. Number of affected rows: " .. tostring(num_affected_rows) .. " Last insert id: " .. tostring(last_insert_id) ) end Link to comment
CocaColaBear Posted October 7, 2013 Author Share Posted October 7, 2013 Since MTA:SA build r5862 you are able to do it without doing any queries after the initial query. Here is the Wiki example. local result, num_affected_rows, last_insert_id = dbPoll ( qh, -1 ) if result == nil then outputConsole( "dbPoll result not ready yet" ) elseif result == false then local error_code,error_msg = num_affected_rows,last_insert_id outputConsole( "dbPoll failed. Error code: " .. tostring(error_code) .. " Error message: " .. tostring(error_msg) ) else outputConsole( "dbPoll succeeded. Number of affected rows: " .. tostring(num_affected_rows) .. " Last insert id: " .. tostring(last_insert_id) ) end Thank you, I've seen) Very good addition Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now