Jump to content

[HELP] Getting the a value from the last INSET INTO SQLite


..:D&G:..

Recommended Posts

Hello guys, is there any way to get the value of the last data inserted using executeSQLQuery("INSERT INTO") like an ID? I want to make a slot machine system that saves in SQLite and I want each slot machine to have an unique id, and I need to get the ID of the latest slot machine created and then +1.

Anybody any ideas?

Link to comment

Same error :/ Here is the whole function...

function createSlotmachine(thePlayer, ...) 
    if thePlayer and isObjectInACLGroup("user." .. getAccountName(getPlayerAccount(thePlayer)), aclGetGroup("Admin")) then 
        local dimension = getElementDimension(thePlayer) 
        local interior = getElementInterior(thePlayer) 
        local x, y, z  = getElementPosition(thePlayer) 
        local rotation = getPedRotation(thePlayer) 
        z = z - 0.3 
        local id = executeSQLQuery("INSERT INTO `slotmachines`(`x`,`y`,`z`,`rotation`,`interior',`dimension`) VALUES(?,?,?,?,?,?)", x, y, z, rotation, interior, dimension ) 
                     
        if (id) then 
            local object = loadSlotMachines(x, y, z+0.58, 0, 0, rotation-180, interior, dimension) 
            setElementData(object, "dbid", id) 
                     
            local px = x + math.sin(math.rad(-rotation)) * 0.8 
            local py = y + math.cos(math.rad(-rotation)) * 0.8 
            local pz = z 
  
            x = x + ((math.cos(math.rad(rotation)))*5) 
            y = y + ((math.sin(math.rad(rotation)))*5) 
            setElementPosition(thePlayer, x, y, z) 
             
            outputChatBox("Slot machine created with ID #" .. id .. "!", thePlayer, 0, 255, 0) 
        else 
            outputChatBox("There was an error while creating a slot machine Try again.", thePlayer, 255, 0, 0) 
        end 
    end 
end 
addCommandHandler("addslotm", createSlotmachine) 

EDIT - Even though there is an error in the syntax, the slot machine is still spawning but is not added into the db. When I spawn it it doesn't say that I did,..

Link to comment

Same error, here is a picture of the error:

5grg1.png

And the new code:

function createSlotmachine(thePlayer, ...) 
    if thePlayer and isObjectInACLGroup("user." .. getAccountName(getPlayerAccount(thePlayer)), aclGetGroup("Admin")) then 
        local dimension = getElementDimension(thePlayer) 
        local interior = getElementInterior(thePlayer) 
        local x, y, z  = getElementPosition(thePlayer) 
        local rotation = getPedRotation(thePlayer) 
        z = z - 0.3 
        local id = executeSQLQuery("INSERT INTO `slotmachines`(`x`,`y`,`z`,`rotation`,`interior',`dimension`) VALUES(??,??,??,??,??,??)", x, y, z, rotation, interior, dimension ) 
        local dbid = executeSQLQuery("SELECT * FROM slotmachines ORDER BY id DESC LIMIT 1") 
        if (id) then 
            local object = loadSlotMachines(x, y, z+0.58, 0, 0, rotation-180, interior, dimension) 
            setElementData(object, "dbid", dbid) 
                     
            local px = x + math.sin(math.rad(-rotation)) * 0.8 
            local py = y + math.cos(math.rad(-rotation)) * 0.8 
            local pz = z 
  
            x = x + ((math.cos(math.rad(rotation)))*5) 
            y = y + ((math.sin(math.rad(rotation)))*5) 
            setElementPosition(thePlayer, x, y, z) 
             
            outputChatBox("Slot machine created with ID #" .. id .. "!", thePlayer, 0, 255, 0) 
        else 
            outputChatBox("There was an error while creating a slot machine Try again.", thePlayer, 255, 0, 0) 
        end 
    end 
end 
addCommandHandler("addslotm", createSlotmachine) 

Link to comment
executeSQLQuery ( "INSERT INTO `slotmachines` ( x, y, z, rotation, interior, dimension ) VALUES ( ?, ?, ?, ?, ?, ? )", x, y, z, rotation, interior, dimension ) 

Finally Thanks! I would also like to ask why I don't get the "Slot Machine created with ID" message. Is it maybe because for the element data "dbid" I get it to be boolean? How can I fix it if so?

Link to comment
That's weird.

Try something like:

outputChatBox ( type ( id ) ) 

Still doesn't show me any message in the chat box

As wiki says:
Returns a table with the result of the query if it was a SELECT query, or false if otherwise.

Your query is always false. So your "if id then" will never actually pass.

What should I do then instead of "if id then"? I am used to MySQL, and that's what I do in MySQL, never tried saving data in SQLite

Link to comment

And how can I make an unique id for every new slot machine spawned?

Or how can I do something like this? :

  
local result = mysqlscr:query("SELECT id, x, y, z, rotation, interior, dimension FROM cows" ) 
local row = mysqlscr:fetch_assoc(result) 
            if not row then break end 
            animals = {} 
            --local s = #animals+1 
             
            local id = tonumber( row["id"] ) 

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