spoty Posted April 27, 2015 Share Posted April 27, 2015 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
MTA Team botder Posted April 27, 2015 MTA Team Share Posted April 27, 2015 https://wiki.multitheftauto.com/wiki/DbExec Returns true unless the connection is incorrect, in which case it returns false. Link to comment
spoty Posted April 27, 2015 Author Share Posted April 27, 2015 already readed over 20 times but i still cant get it fixed 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 botder Posted April 27, 2015 MTA Team Share Posted April 27, 2015 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
spoty Posted April 27, 2015 Author Share Posted April 27, 2015 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
Addlibs Posted April 27, 2015 Share Posted April 27, 2015 dbExec won't return any data. You need to use dbQuery with dbPoll Link to comment
spoty Posted April 27, 2015 Author Share Posted April 27, 2015 verry confusing right now i hear from alot of ppl use dbExec and from other dbQuery Link to comment
Walid Posted April 28, 2015 Share Posted April 28, 2015 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now