Jump to content

[HELP]SQLite database


spoty

Recommended Posts

hey i am verry noobish at SQLite database scripting can someone help me?

if i use this code

    local check = dbExec ( connection, "SELECT `data` FROM `drift` WHERE `account`=?", accName ), -1 ; -- checking if exist row 
    if ( #check > 0 ) then 
        dbExec ( connection, "UPDATE `drift` SET `data`=? WHERE `account`=?", toJSON ( t ), accName ); -- if yes updating 
    elseif ( #check == 0 ) then 
        dbExec ( connection, "INSERT INTO `drift` ( `data`, `account` ) VALUES ( ?, ? )", toJSON ( t ), accName ); -- if not creating 
    end 
end 
addEventHandler ( "onPlayerQuit", root, saveDriftPoints ); 

i get this error

ERROR 2: attempt to get lenght of local 'check' (a boolean value)

i dont know what i am doing wrong over here...

i used this before

    local check = dbPoll ( dbQuery ( connection, "SELECT `data` FROM `drift` WHERE `account`=?", accName ), -1 ); -- checking if exist row 
    if ( #check > 0 ) then 
        dbQuery ( connection, "UPDATE `drift` SET `data`=? WHERE `account`=?", toJSON ( t ), accName ); -- if yes updating 
    elseif ( #check == 0 ) then 
        dbQuery ( connection, "INSERT INTO `drift` ( `data`, `account` ) VALUES ( ?, ? )", toJSON ( t ), accName ); -- if not creating 
    end 
end 
addEventHandler ( "onPlayerQuit", root, saveDriftPoints ); 

that was working fine but it dind what it should need to do

so i asked help and someone told me to use

dbExec --- inplace off 
dbQuery 
Link to comment

already readed over 20 times but i still cant get it fixed :S

and i know

Returns

Returns true unless the connection is incorrect, in which case it returns false.

but its all about this line

if ( #check > 0 ) then 

that is giving a boolean value

Link to comment
  • MTA Team
attempt to get length of local 'check' (a boolean value)

The variable check is a boolean value. Using the operator '#' on it is illegal. That operator tries to get the length of strings and tables.

#check 

This won't work - the error tells you that.

if ( #check > 0 ) then 

In conclusion this will not work at all with 'check' as a boolean value.

Link to comment
attempt to get length of local 'check' (a boolean value)

The variable check is a boolean value. Using the operator '#' on it is illegal. That operator tries to get the length of strings and tables.

#check 

This won't work - the error tells you that.

if ( #check > 0 ) then 

In conclusion this will not work at all with 'check' as a boolean value.

how to make a other working check then?

i using it to check if there is a table if not then it creates one

and to check if its a valid account

Link to comment

Simply you can use something like that :

* Connect

local driftdb = "drift.db"  
local connection = dbConnect("sqlite", driftdb) 
  
function drift() 
    dbExec(connection, "CREATE TABLE IF NOT EXISTS drift (account TEXT, data TINYTEXT)") 
end  
addEventHandler("onResourceStart", resourceRoot,drift) 

*checking if exist row

function doesAccountExistInDriftList(account) 
    local check = dbPoll(dbQuery(connection, "SELECT * FROM drift WHERE account = ?",tostring(account)), -1) 
        if type(check) == "table" and #check == 0 or not check then 
        return false 
    else 
        return true 
    end 
end 

* insert new row (account)

function addNewAccount(account,drift) 
    local check = dbPoll(dbQuery(connection, "SELECT * FROM drift WHERE account = ?",tostring(account)), -1) 
        if type(check) == "table" and #check == 0 or not check then 
            dbExec(connection, "INSERT INTO drift VALUES (?, ?)",tostring(account), tonumber(drift),1) 
                return true 
            else 
        return false 
    end 
end 

*update drift points

function updateDriftPoints(account,drift) 
    local check = dbPoll(dbQuery(connection, "SELECT * FROM drift WHERE account = ?",tostring(account)), -1) 
        if type(check) ~= "table" or #check ~= 0 or check then 
            if dbExec(connection, "UPDATE drift SET data = ? WHERE account = ?",tonumber(drift),tostring(account)) then 
                return true 
            else 
        return false 
    end 
  end 
end 

just check if the account name exist using "doesAccountExistInDriftList()" if yes update drift points using "updateDriftPoints()" function if no insert new row using "addNewAccount().

Note: in this case it will just save the last points so if you want to add them to the old points without replacing the old points with new points , you need to check the player current points before you call updateDriftPoints() function.

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