Image Posted June 6, 2013 Posted June 6, 2013 Hey. I want to switch from MTA-MySQL module to MTA's own mysql system. Is there any method to retrieve the insert id? For example: local query = dbExec(connection, "INSERT INTO vehicles (model, posX, posY, posZ, posRX, posRY, posRZ, color1, color2) VALUES (?,?,?,?,?,?,?,?,?)", tonumber(model), vehicleData.posX, vehicleData.posY, vehicleData.posZ, vehicleData.posRX, vehicleData.posRY, vehicleData.posRZ, tonumber(color1), tonumber(color2)) SQL generates automatically the vehicleID column (Auto increment) I want to recieve this ID. Can anybody help me? P.S.: My english skill sucks so sorry for mistakes.
DiSaMe Posted June 6, 2013 Posted June 6, 2013 http://dev.mysql.com/doc/refman/5.7/en/ ... -insert-id
Image Posted June 6, 2013 Author Posted June 6, 2013 It returns nil. Code: local query = dbQuery(connection, "SELECT LAST_INSERT_ID()") local result = dbPoll(query, 10) outputDebugString(result)
DiSaMe Posted June 6, 2013 Posted June 6, 2013 Of course it does, that means 10 ms isn't enough to get the result. In theory, no amount of time can guarantee that the result will be ready. If you want the result to be always returned, make the waiting time unlimited: local result = dbPoll(query, -1)
Image Posted June 6, 2013 Author Posted June 6, 2013 It returns a table. How can i extract it? local query = dbQuery(connection, "SELECT LAST_INSERT_ID()") local result = dbPoll(query, -1) for result, row in pairs ( result ) do outputDebugString(row) end It isn't works.
Castillo Posted June 6, 2013 Posted June 6, 2013 @Image: If you use "-1" to wait until it got a result, then I recommend you to use callbacks.
DiSaMe Posted June 7, 2013 Posted June 7, 2013 It shows what the table looks like in the examples of dbPoll page. The value is in table[row_number]["column_name"]. In your case, it's probably like this: result[row]["LAST_INSERT_ID()"] To give the column another name (so that the expression string would not be used for it), execute a query with explicitly specified column name: local query = dbQuery(connection, "SELECT LAST_INSERT_ID() AS id") And the result will be: result[row].id
Image Posted June 7, 2013 Author Posted June 7, 2013 @Image: If you use "-1" to wait until it got a result, then I recommend you to use callbacks. It shows what the table looks like in the examples of dbPoll page. The value is in table[row_number]["column_name"]. In your case, it's probably like this: result[row]["LAST_INSERT_ID()"] To give the column another name (so that the expression string would not be used for it), execute a query with explicitly specified column name: local query = dbQuery(connection, "SELECT LAST_INSERT_ID() AS id") And the result will be: result[row].id It works now. Thanks.
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