Jump to content

[help] sqlite can't save proper


Recommended Posts

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
Link to comment
  
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) 
  
  

Link to comment

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

Link to comment
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

Link to comment
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) 

Link to comment
  
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) 
  

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