Jump to content

MySQL


Image

Recommended Posts

Posted

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.

Head Developer of SeeMTA

Founder and Head Developer of SAS-Network

560x95_FFFFFF_FF9900_000000_000000.png]
Posted

It returns nil.

Code:

local query = dbQuery(connection, "SELECT LAST_INSERT_ID()") 
local result = dbPoll(query, 10) 
outputDebugString(result) 

Head Developer of SeeMTA

Founder and Head Developer of SAS-Network

560x95_FFFFFF_FF9900_000000_000000.png]
Posted

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) 

-

Posted

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.

Head Developer of SeeMTA

Founder and Head Developer of SAS-Network

560x95_FFFFFF_FF9900_000000_000000.png]
Posted

@Image: If you use "-1" to wait until it got a result, then I recommend you to use callbacks.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

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 

-

Posted
@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.

Head Developer of SeeMTA

Founder and Head Developer of SAS-Network

560x95_FFFFFF_FF9900_000000_000000.png]

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...