Jump to content

SQLite inserting ID


JanriKallas

Recommended Posts

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)

Link to comment

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 

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