Jump to content

SQLite inserting ID


JanriKallas

Recommended Posts

Posted

When I insert new row(?) to table, I want each row has their own ID

Should i do it like this?

  
ID = 0 
  
function() 
    if (dbExec( connection, "INSERT INTO abc (id,x,y,z) VALUES (?,?,?,?)",ID,x,y,z))) then  
       ID = ID+1 
    end 
end 
  

or how?

Posted

I found this

  
CREATE SEQUENCE SEQ_USER START WITH 1 INCREMENT BY 1; 
We will specify that we want to use the sequence and the NEXTVAL function in the INSERT INTO statement as follows: 
  
INSERT INTO USER_TABLE VALUES (SEQ_USER.NEXTVAL, 'Washington', 'George');  
  

so i tried

  
dbExec("CREATE SEQUENCE SEQ_USER START WITH 1 INCREMENT BY 1") 
dbExec(connection, "INSERT INTO USER_TABLE VALUES (?,?,?,?)",SEQ_USER .NEXTVAL,x,y,z) 
  

but it gets only errors

attempt to index global "SEQ_USER" (a nil value)

Posted

Problem solved..

  
dbExec(CREATE TABLE USER_TABLE (id INT NOT NULL AUTO_INCREMENT, x INT, y INT, z INT, PRIMARY KEY (id)) 
  

so everytime you insert it automatically puts an ID

Posted

Try to use sth like this :

local db = dbConnect("sqlite", "databaseName.db") -- Put your database here. 
  
function getNewID() 
    local result = dbPoll(dbQuery(db, "SELECT ID FROM YourTable ORDER BY ID ASC"), -1) 
        newID = false 
            for i, id in pairs (result) do 
                if id["ID"] ~= i then -- ID column 
                newID = i 
            break 
        end 
    end 
    if newID then  
        return newID  
    else return #result + 1  
  end 
end 

Do not yield your back to your enemy, might feel something strange in your ass.

Two things are infinite the universe and human stupidity and i'm not sure about the universe.

UF: IsTextInGridList | GetGridListRowIndexFromText | Table.removeValue | removeHex | dxDrawTriangle

Skype: SaSuki102 | About Me | Youtube channel | Lua Tips & Tricks | Lua Strings | Lua Tables | Lua Operators

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