Jump to content

dbQuery ?


pcisoft

Recommended Posts

Hello,

I'm Creating a Repair-Kit System, While tried to setAccountData to store repair kits it doesn't work

I want repair kits to be saved as Element Data and I replaced the Account data with Database

Code:

function saveRepairKits(player, kits) 
local acc = getPlayerAccount(player) 
local accname = getAccountName(acc) 
local cur = getElementData(player, "Repair.Kits") 
    local itemnames = val3.amount 
    if cur ~= nil then  
    dbExec (db, "UPDATE repairkits SET amount=? WHERE account=?", tonumber(cur), accname) 
    else 
    dbExec (db, "INSERT INTO repairkits (account, amount) VALUES (?, ?)",accname, tonumber(cur)) 
    end 
end 
  
function LoadRepairs (player, kits) 
local kitt = dbPoll ( dbQuery ( db, "SELECT * FROM repairkits WHERE account=?" ), getAccountName(getPlayerAccount(player)), -1 )  
setElementData(player, "Repair.Kits", kitt) 
end 
  
addEventHandler ( "onPlayerLogin", rootElement, LoadRepairs ) 
addEventHandler ( "onPlayerLogout", rootElement, saveRepairKits ) 

That's my storation code that doesn't work, that's my first Database script

can anyone tell me how to load repairkits from db and set it as the player's data

and when he logout save his repairkits in database (UPDATE if exsists or INSERT INTO if doesn't exist in table)

Thanks!!

Link to comment

try this :

function saveRepairKits( acc ) 
    local accname = getAccountName(acc) 
    local cur = getElementData(source, "Repair.Kits") 
    local itemnames = val3.amount 
    if cur then 
    local results = dbPoll( dbQuery( db, "SELECT * FROM repairkits WHERE account=?", accname ), -1 ) 
    if results and type(results) == "table" and #results > 0 then return dbExec( db, "UPDATE repairkits SET amount=? WHERE account=?", tonumber( cur ), accname )  end 
    dbExec (db, "INSERT INTO repairkits (account, amount) VALUES (?, ?)",accname, tonumber(cur)) 
    end 
end 
  
function LoadRepairs ( _, acc ) 
    local kitt = dbPoll ( dbQuery ( db, "SELECT * FROM repairkits WHERE account=?", getAccountName( acc )), -1 ) 
        if kitt then 
            for k,v in ipairs ( kitt ) do 
                setElementData(source, "Repair.Kits", v['amount']) 
        end 
    end 
end 
  
addEventHandler ( "onPlayerLogin", root, LoadRepairs ) 
addEventHandler ( "onPlayerLogout", root, saveRepairKits ) 

note: line 4 isn't defined in this code .

Link to comment
try this :

function saveRepairKits( acc ) 
    local accname = getAccountName(acc) 
    local cur = getElementData(source, "Repair.Kits") 
    local itemnames = val3.amount 
    if cur then 
    local results = dbPoll( dbQuery( db, "SELECT * FROM repairkits WHERE account=?", accname ), -1 ) 
    if results and type(results) == "table" and #results > 0 then return dbExec( db, "UPDATE repairkits SET amount=? WHERE account=?", tonumber( cur ), accname )  end 
    dbExec (db, "INSERT INTO repairkits (account, amount) VALUES (?, ?)",accname, tonumber(cur)) 
    end 
end 
  
function LoadRepairs ( _, acc ) 
    local kitt = dbPoll ( dbQuery ( db, "SELECT * FROM repairkits WHERE account=?", getAccountName( acc )), -1 ) 
        if kitt then 
            for k,v in ipairs ( kitt ) do 
                setElementData(source, "Repair.Kits", v['amount']) 
        end 
    end 
end 
  
addEventHandler ( "onPlayerLogin", root, LoadRepairs ) 
addEventHandler ( "onPlayerLogout", root, saveRepairKits ) 

note: line 4 isn't defined in this code .

Haven't Worked :/

It doesn't save anything :/

local db = dbConnect("sqlite", "database.db") 
  
  
dbExec (db, "CREATE TABLE IF NOT EXISTS repairkits (account, amount)") 
  
  
function saveRepairKits( acc ) 
    local accname = getAccountName(acc) 
    local cur = getElementData(source, "Repair.Kits") 
    if cur then 
    local results = dbPoll( dbQuery( db, "SELECT * FROM repairkits WHERE account=?", accname ), -1 ) 
    if results and type(results) == "table" and #results > 0 then return dbExec( db, "UPDATE repairkits SET amount=? WHERE account=?", tonumber( cur ), accname )  end 
    dbExec (db, "INSERT INTO repairkits (account, amount) VALUES (?, ?)",accname, tonumber(cur)) 
    end 
end 
  
function LoadRepairs ( _, acc ) 
    local kitt = dbPoll ( dbQuery ( db, "SELECT * FROM repairkits WHERE account=?", getAccountName( acc )), -1 ) 
        if kitt then 
            for k,v in ipairs ( kitt ) do 
                setElementData(source, "Repair.Kits", v['amount']) 
        end 
    end 
end 
  
addEventHandler ( "onPlayerLogin", root, LoadRepairs ) 
addEventHandler ( "onPlayerLogout", root, saveRepairKits ) 

Anyhelp please

have you tested your script ?

Link to comment

Try this untested i'm using the phone

local db = dbConnect("sqlite", "database.db") 
dbExec (db, "CREATE TABLE IF NOT EXISTS repairkits (account, amount)") 
  
 -- save repair kits 
function saveRepairKits(player) 
    if player and isElement(player) then  
        local account = getPlayerAccount(player) 
        if isGuestAccount (account) then 
            local accountName = getAccountName(account) 
            local cur = getElementData(player, "Repair.Kits") or 0 
            if cur then 
                local results = dbPoll( dbQuery( db, "SELECT * FROM repairkits WHERE account = ?", accountName), -1 ) 
                if results and type(results) == "table" and #results > 0 then   
                    dbExec( db, "UPDATE repairkits SET amount = ? WHERE account = ?", tonumber(cur),accountName)  
                else 
                    dbExec (db, "INSERT INTO repairkits (account, amount) VALUES (?, ?)",accountName, tonumber(cur)) 
                end  
            end  
        end  
    end 
end 
  
  
function saveOnQuit() 
    saveRepairKits(source) 
end  
addEventHandler ( "onPlayerQuit", root, saveRepairKits ) 
addEventHandler ( "onPlayerLogout", root, saveRepairKits ) 
  
-- load repair kits 
function LoadRepairs ( _, acc ) 
    local accountName = getAccountName(acc) 
    local tab = getAccountrepairKits(accountName) or 0 
    if tab then  
        setElementData(source, "Repair.Kits", tonumber(tab)) 
    end  
end  
addEventHandler ( "onPlayerLogin", root, LoadRepairs ) 
  
-- get Account repair kits 
function getAccountRepairKits(accName) 
    local results = dbPoll(dbQuery(db, "SELECT * FROM repairkits WHERE account = ?",tostring(accName)), -1) 
    if type(results) == "table" and #results == 0 or not results then 
        return false 
    else 
        return results[1]["amount"] 
    end 
end  

Link to comment
Try this untested i'm using the phone
local db = dbConnect("sqlite", "database.db") 
dbExec (db, "CREATE TABLE IF NOT EXISTS repairkits (account, amount)") 
  
 -- save repair kits 
function saveRepairKits(player) 
    if player and isElement(player) then  
        local account = getPlayerAccount(player) 
        if isGuestAccount (account) then 
            local accountName = getAccountName(account) 
            local cur = getElementData(player, "Repair.Kits") or 0 
            if cur then 
                local results = dbPoll( dbQuery( db, "SELECT * FROM repairkits WHERE account = ?", accountName), -1 ) 
                if results and type(results) == "table" and #results > 0 then   
                    dbExec( db, "UPDATE repairkits SET amount = ? WHERE account = ?", tonumber(cur),accountName)  
                else 
                    dbExec (db, "INSERT INTO repairkits (account, amount) VALUES (?, ?)",accountName, tonumber(cur)) 
                end  
            end  
        end  
    end 
end 
  
  
function saveOnQuit() 
    saveRepairKits(source) 
end  
addEventHandler ( "onPlayerQuit", root, saveRepairKits ) 
addEventHandler ( "onPlayerLogout", root, saveRepairKits ) 
  
-- load repair kits 
function LoadRepairs ( _, acc ) 
    local accountName = getAccountName(acc) 
    local tab = getAccountrepairKits(accountName) or 0 
    if tab then  
        setElementData(source, "Repair.Kits", tonumber(tab)) 
    end  
end  
addEventHandler ( "onPlayerLogin", root, LoadRepairs ) 
  
-- get Account repair kits 
function getAccountRepairKits(accName) 
    local results = dbPoll(dbQuery(db, "SELECT * FROM repairkits WHERE account = ?",tostring(accName)), -1) 
    if type(results) == "table" and #results == 0 or not results then 
        return false 
    else 
        return results[1]["amount"] 
    end 
end  

Ya3tik essa7a

wallahi 5sartou aka COG fera8, d5altlou 9olt hetha chyetla3 Tefeh ye5i tla3 mezyen barcha, 5sartou w barra :/

---------------------------------------------------------TRANSLATE TO ENGLISH------------------------------------------------------

Thanks you

Im sad about COG cauz It's empty, I visited it and thinked that it's a noob server but found that they are good Scripts Im sad about it :/

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