Jump to content

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


..:D&G:..

Recommended Posts

Posted

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?

Posted

SELECT * FROM yourtable ORDER BY id DESC LIMIT 1 

Should work if you have id column (i think MySQL automatically defines one, SQLite should do that too, not sure).

If you don't have an id, you can add a column with AUTO_INCREMENT type.

Posted

CREATE TABLE yourtable (id INT PRIMARY KEY AUTO_INCREMENT NOT NULL , <your other columns>) 

Or change the table you have

ALTER TABLE yourtable MODIFY COLUMN id INT AUTO_INCREMENT 

(2nd example changes a column to be auto increment, but doesn't add it)

Posted

I get syntax error near "dimension"

local id = executeSQLQuery("INSERT INTO `slotmachines`(`id`,`x`,`y`,`z`,`rotation`,`interior',`dimension`) VALUES(?,?,?,?,?,?,?)", id, x, y, z, rotation, interior, dimension ) 
         

Posted

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

Posted

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) 

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

Posted
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?

Posted

Using outputChatBox it doesn't return anything, any other ways I can check?

It also looks like the machines spawn when I restart the script but they do not appear.

Posted
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

Posted

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"] ) 

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