Jump to content

[HELP] sqlite


AshFTW

Recommended Posts

Posted

I have connection to db with this content:

+--+----+---+ 
|id|name|hp | 
+--+----+---+ 
|1 |mom |82 | 
|2 |guy |74 | 
|5 |gay |69 | 
|6 |lol |100| 
|7 |sas |93 | 
+--+----+---+ 

So, I wanna to get last available id, it will be 3, then 4, then 8. So, how to write this code?

Also I'm thying this code, but get error

    local q = dbQuery(connect, "SELECT MAX(`id`) FROM `tabname`") 
    res = dbPoll (q, -1) 
    dbFree(q) 
    for k, i in ipairs (res) do 
        outputChatBox("next id is "..res[k]['id']+1) 
[...] 

Posted

If you set AUTO_INCREMENT on the id field on that table, just pass NULL to the id field when executing an INSERT query and it will auto increment the id. If you want to get the ID which the db incremented to your query use this:

  
local _, _, last_insert_id = dbPoll(dbQuery(CONNECTION_HANDLER, QUERY), -1) 
  

Another way to get the next ID is:

  
local table = dbPoll(...) 
local next_id = table[#table]["id"]+1 
  

350x20_FFFFFF_FFFFFF_000000_000000.png

http://i.imgur.com/CSE28MJ.png

Don't hesitate to contact me for anything! If I can help, I will for sure!

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

Posted

Oh, thanx Silva

Should I use dbFree()?

So this query to create the table is correct?

    dbExec(connect, "CREATE TABLE IF NOT EXISTS `accounts` (`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `nickname` TEXT, `password` TEXT, `email` TEXT);") 

or i shoud use just "`id` INTEGER AUTOINCREMENT"?

Posted

hope this will help you

  
local insertQuery = dbQuery(handler,"SELECT last_insert_rowid() FROM `tablename`") 
                        local iq = dbPoll(insertQuery,-1) 
                        local insertID = iq[1]["last_insert_rowid()"] 
  
  

Script Trading Status

Successful Trading : 26

Scam : 0

On Sale : Banking System SQL Based

On Sale : Housing System MySQL Based

Download and Support my new script on Community : http://community.mtasa.com/index.php?p=resources&s=details&id=11686

SQL Based Housing

Posted
Oh, thanx Silva

Should I use dbFree()?

So this query to create the table is correct?

    dbExec(connect, "CREATE TABLE IF NOT EXISTS `accounts` (`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `nickname` TEXT, `password` TEXT, `email` TEXT);") 

or i shoud use just "`id` INTEGER AUTOINCREMENT"?

well i dont usually create tables using sql language i let the hard work to phpmyadmin but i am sure u will find on google

dbFree is not needed when the query is done by dbExec or handled with dbPoll

350x20_FFFFFF_FFFFFF_000000_000000.png

http://i.imgur.com/CSE28MJ.png

Don't hesitate to contact me for anything! If I can help, I will for sure!

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

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