LabiVila Posted July 7, 2015 Share Posted July 7, 2015 (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 July 7, 2015 by Guest Link to comment
darhal Posted July 7, 2015 Share Posted July 7, 2015 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
LabiVila Posted July 7, 2015 Author Share Posted July 7, 2015 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? Link to comment
LabiVila Posted July 7, 2015 Author Share Posted July 7, 2015 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? Link to comment
darhal Posted July 7, 2015 Share Posted July 7, 2015 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
LabiVila Posted July 7, 2015 Author Share Posted July 7, 2015 Okay, awesome, thank you so much Really appreciate it Link to comment
LabiVila Posted July 8, 2015 Author Share Posted July 8, 2015 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
darhal Posted July 8, 2015 Share Posted July 8, 2015 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
LabiVila Posted July 8, 2015 Author Share Posted July 8, 2015 Thanks but it still does the same... it keeps adding rows in the table, not updating the current one (first one)... Link to comment
darhal Posted July 8, 2015 Share Posted July 8, 2015 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
LabiVila Posted July 8, 2015 Author Share Posted July 8, 2015 Fully working, thank you so much 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