Jump to content

[help] sqlite can't save proper


Recommended Posts

Posted (edited)
local connect = dbConnect ("sqlite", "file.db") 
  
function saveP () 
    local x,y,z = getElementPosition (source) 
    local pos = dbExec (connect, "CREATE TABLE IF NOT EXISTS `position` (`x` float, `y` float, `z` float)") 
    if pos then 
        local q = dbQuery (connect, "INSERT INTO `position` VALUES (?, ?, ?)", x, y, z) 
    else 
        local qq = dbExec (connect, "UPDATE `position` SET x = "..x..", y = "..y..", z = "..z) 
    end 
end 
addEventHandler ("onPlayerQuit", getRootElement(), saveP) 
  
function loadP () 
    local result = dbPoll (dbQuery (connect, "SELECT * FROM `position` WHERE x = "..x.." y = "..y..", z = "..z..")"), -1) 
    if reult then 
        setElementPosition (source, x, y, z) 
        outputChatBox ("SUCCEEED") 
    else 
        outputChatBox ("FAIL") 
    end 
end 
addEventHandler ("onPlayerJoin", getRootElement(), loadP) 
  

Hey, so I recently got stuck here.

What am I doing wrong? is it this line?

local result = dbPoll (dbQuery (connect, "SELECT * FROM `position` WHERE x = "..x.." y = "..y..", z = "..z..")"), -1)

I just want to save player's last coords when je quits and load when he joins...

Edited by Guest
Posted
  
local connect = dbConnect ("sqlite", "file.db") 
  
function saveP () 
    local x,y,z = getElementPosition (source) 
    local pos = dbExec (connect, "CREATE TABLE IF NOT EXISTS `position` (`x` float, `y` float, `z` float)") 
    if pos then 
        local q = dbQuery (connect, "INSERT INTO `position` VALUES (?, ?, ?)", x, y, z) 
    else 
        local qq = dbQuery (connect, "UPDATE `position` SET x = "..x..", y = "..y..", z = "..z) 
    end 
end 
addEventHandler ("onPlayerQuit", getRootElement(), saveP) 
  
function loadP () 
    local query = dbQuery ( connect, "SELECT * FROM position") 
    local result = dbPoll (query, - 1 ) 
for i , v in pairs(result) do 
        setElementPosition (source, v.x, v.y, v.z) 
        outputChatBox ("SUCCEEED") 
    end 
end 
addEventHandler ("onPlayerJoin", getRootElement(), loadP) 
  
  

Posted

Working like a charm, thank you very much.

By the way,

local query = dbQuery ( connect, "SELECT * FROM position") - this gets all columns in the table right? Or what did you exactly do there?

Posted

It's awesome, thank you so much. I have one more question, how can I check if there's the row added, (row 1) I keep updating that one without adding a row 2 as I don't need that?

Posted

You should connect the database and get the table result then you calculate it lenght using # if the lenght is 0 then there is no row aalso you can check if the table is nil or not if it s nil then there is no table in database

Posted
local connect = dbConnect ("sqlite", "file.db") 
  
function saveP () 
    local x,y,z = getElementPosition (source) 
    local pos = dbQuery (connect, "CREATE TABLE IF NOT EXISTS `position` (`x` float, `y` float, `z` float)") 
    local qres = dbPoll (pos, -1) 
    if (#qres == 0) then 
        local q = dbQuery (connect, "INSERT INTO `position` VALUES (?, ?, ?)", x, y, z) 
    else 
        local qq = dbQuery (connect, "UPDATE `position` SET `x`="..x..", `y`="..y..", `z`="..z) 
    end 
end 
addEventHandler ("onPlayerQuit", getRootElement(), saveP) 
  
function loadP () 
    local query = dbQuery ( connect, "SELECT * FROM position") 
    local result = dbPoll (query, -1) 
    for i,v in pairs (result) do 
        setElementPosition (source, v.x, v.y, v.z) 
        outputChatBox ("You have been wrapped to your last position.", soure, 255, 255, 255) 
    end 
end 
addEventHandler ("onPlayerJoin", getRootElement(), loadP) 

What's not working? I really am handling this for quite a while but.. it seems cancerious

Posted
local connect = dbConnect ("sqlite", "file.db") 
  
function saveP () 
    local x,y,z = getElementPosition (source) 
    local pos = dbQuery (connect, "CREATE TABLE IF NOT EXISTS `position` (`x` float, `y` float, `z` float)") 
    local qres = dbPoll (pos, -1) 
    if (#qres == 0) then 
        local q = dbQuery (connect, "INSERT INTO `position` VALUES (?, ?, ?)", x, y, z) 
    else 
        local qq = dbQuery (connect, "UPDATE `position` SET `x`="..x..", `y`="..y..", `z`="..z) 
    end 
end 
addEventHandler ("onPlayerQuit", getRootElement(), saveP) 
  
function loadP () 
    local query = dbQuery ( connect, "SELECT * FROM position") 
    local result = dbPoll (query, -1) 
    for i,v in pairs (result) do 
        setElementPosition (source, v.x, v.y, v.z) 
        outputChatBox ("You have been wrapped to your last position.", soure, 255, 255, 255) 
    end 
end 
addEventHandler ("onPlayerJoin", getRootElement(), loadP) 

What's not working? I really am handling this for quite a while but.. it seems cancerious

]

local connect = dbConnect ("sqlite", "file.db") 
  
function saveP () 
    local x,y,z = getElementPosition (source) 
    local pos = dbQuery (connect, "CREATE TABLE IF NOT EXISTS `position` (`x` float, `y` float, `z` float)") 
    local qres = dbPoll (pos, -1) 
    if (#qres == 0) or not qres or type(qres) ~= "table" then 
        local q = dbQuery (connect, "INSERT INTO `position` VALUES (?, ?, ?)", x, y, z) 
    else 
        local qq = dbQuery (connect, "UPDATE `position` SET `x`="..x..", `y`="..y..", `z`="..z) 
    end 
end 
addEventHandler ("onPlayerQuit", getRootElement(), saveP) 
  
function loadP () 
    local query = dbQuery ( connect, "SELECT * FROM position") 
    local result = dbPoll (query, -1) 
    for i,v in pairs (result) do 
        setElementPosition (source, v.x, v.y, v.z) 
        outputChatBox ("You have been wrapped to your last position.", soure, 255, 255, 255) 
    end 
end 
addEventHandler ("onPlayerJoin", getRootElement(), loadP) 

Posted
  
local connect = dbConnect ("sqlite", "file.db") 
  
function saveP () 
    local x,y,z = getElementPosition (source) 
    local pos = dbQuery (connect, "CREATE TABLE IF NOT EXISTS `position` (`x` float, `y` float, `z` float)") 
    local query = dbQuery ( connect, "SELECT * FROM position") 
    local result = dbPoll (query, -1) 
    if (#result == 0) or not result or type(result) ~= "table" then 
        local q = dbQuery (connect, "INSERT INTO `position` VALUES (?, ?, ?)", x, y, z) 
    else 
        local qq = dbQuery (connect, "UPDATE `position` SET `x`="..x..", `y`="..y..", `z`="..z) 
    end 
end 
addEventHandler ("onPlayerQuit", getRootElement(), saveP) 
  
function loadP () 
    local query = dbQuery ( connect, "SELECT * FROM position") 
    local result = dbPoll (query, -1) 
    for i,v in pairs (result) do 
        setElementPosition (source, v.x, v.y, v.z) 
        outputChatBox ("You have been wrapped to your last position.", soure, 255, 255, 255) 
    end 
end 
addEventHandler ("onPlayerJoin", getRootElement(), loadP) 
  

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