Jump to content

MySQL


Image

Recommended Posts

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.

Link to comment

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) 

Link to comment

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.

Link to comment

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 

Link to comment
@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.

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...