Jump to content

SQLite HELP


Hero192

Recommended Posts

Hey guys i m trying to do a saver , position,money and account but it doesn't works plus no error, please i need someone to give me a hand i stuck on this point,

Here's my code!

local connection = exports.sql:getSQLConnection() 
addEventHandler("onResourceStart",resourceRoot, 
function()  
    dbExec(connection, "CREATE TABLE IF NOT EXISTS accountdata ( account TEXT, x TEXT, y TEXT, z TEXT, money INTEGER)") 
end) 
    
 function saveDataBase() 
    local account = getPlayerAccount ( source ) 
    local money = getPlayerMoney ( source ) 
    local x, y, z = getElementPosition ( source ) 
        if account and not isGuestAccount(account) then 
            local accountName = getAccountName(account) 
            if AccountExist ( accountName ) then 
            dbExec ( connection, "INSERT INTO accountdata (x, y, z, money, account ) VALUES ( ?, ?, ?, ?, ?, ? )", x, y, z, money, accountName) 
        end 
    end 
end 
  
  
function AccountExist ( AccountName ) 
    local result = dbPoll( dbQuery ( connection, "SELECT * FROM accountdata WHERE account = ?", tostring (AccountName)),-1) 
        if ( type ( result ) == "table" and #result == 0 or not result ) then 
            return false 
        else 
        return true 
    end 
end 
  
 function loadDataBase (  ) 
    local account = getPlayerAccount ( source ) 
    local accountName = getAccountName(account) 
    local data = AccountExist(accountName) 
    if ( data and type ( data ) == 'table' ) then 
    for index, val in ipairs  ( data ) do 
     if ( val['account'] == account ) then 
     local x = tonumber ( val['x'] )  
     local y = tonumber ( val['y'] )  
     local z = tonumber ( val['z'] )   
     local money = tonumber ( val['money'] )  
     givePlayerMoney ( source, money ) 
     setElementPosition (source, x, y, z ) 
        end  
      end  
   end 
end 
  
addEventHandler("onPlayerLogin", root, loadDataBase) 
addEventHandler("onPlayerQuit", root, saveDataBase) 
addEventHandler("onPlayerLogout", root, saveDataBase) 

Link to comment

Try:

local connection = exports.sql:getSQLConnection() 
  
addEventHandler("onResourceStart", resourceRoot, 
function() 
    dbExec(connection, "CREATE TABLE IF NOT EXISTS accountdata (account TEXT, x TEXT, y TEXT, z TEXT, money INTEGER)") 
end) 
    
function saveDataBase(account) 
    local account = eventName == "onPlayerLogout" and account or getPlayerAccount(source) 
    if account and not isGuestAccount(account) then 
        local money = getPlayerMoney(source) 
        local x, y, z = getElementPosition(source) 
        local accountName = getAccountName(account) 
        if AccountExist(accountName) then 
            dbExec(connection, "UPDATE accountdata SET x = ?, y = ?, z = ?, money = ? WHERE account = ?", x, y, z, money, accountName) 
        else 
            dbExec(connection, "INSERT INTO accountdata VALUES (?, ?, ?, ?, ?)", accountName, x, y, z, money) 
        end 
    end 
end 
  
  
function AccountExist(accountName) 
    local result = dbPoll(dbQuery(connection, "SELECT x, y, z, money FROM accountdata WHERE account = ? LIMIT 1", accountName),-1) 
    if type(result) == "table" and #result > 0 then 
        return result[1] 
    end 
    return false 
end 
  
function loadDataBase(_, account) 
    local data = AccountExist(getAccountName(account)) 
    if data then 
        setPlayerMoney(source, data["money"]) 
        spawnPlayer(source, data["x"], data["y"], data["z"]) 
        fadeCamera(source, true) 
        setCameraTarget(source) 
    end 
end 
addEventHandler("onPlayerLogin", root, loadDataBase) 
addEventHandler("onPlayerQuit", root, saveDataBase) 
addEventHandler("onPlayerLogout", root, saveDataBase) 

Edited by Guest
Link to comment

It's working thanks alot but i need to equal in the load part if the account in sqlite table equal to the player account it doesn't works if i did that, and i need this part to do something

function loadDataBase(_, account) 
    local data = AccountExist(getAccountName(account)) 
    if data then 
    if data["account"] == getAccountName(account) then 
        setPlayerMoney(source, data["money"]) 
        spawnPlayer(source, data["x"], data["y"], data["z"]) 
        fadeCamera(source, true) 
        setCameraTarget(source) 
         end 
    end 
end 

Link to comment

It's already been checked with SELECT, as you can see here it only select rows that have 'account' column equal to the account name and it limited to only one row as we already know there won't be more than one row for each account name.

cAMmC9l.png

It return nil because i didn't select the account name as it not needed, you already know it as you have pass it to the SQL query.

Link to comment
It's already been checked with SELECT, as you can see here it only select rows that have 'account' column equal to the account name and it limited to only one row as we already know there won't be more than one row for each account name.

cAMmC9l.png

It return nil because i didn't select the account name as it not needed, you already know it as you have pass it to the SQL query.

Now, i understand well thanks again and for your time to explain this

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