Baseplate Posted June 9, 2015 Share Posted June 9, 2015 (edited) function spawnDespawnCar(p, carName) local accName = getAccountName(getPlayerAccount(p)) local carID = getVehicleModelFromName(carName) local result = executeSQLQuery("SELECT * FROM vehicles WHERE accName=? and ID=?", accName, carID) if (not getElementData (p,"System.Vehicle")) then local upgrades = fromJSON(result[1]["upgrades"]) local veh = createVehicle(...) addVehicleUpgrade(veh, upgrades) setElementData (p, "System.Vehicle", veh) outputChatBox("Your car has been spawned!", p, 0, 255, 0) elseif (getElementData (p, "System.Vehicle")) then local nveh = getElementData (p, "System.Vehicle") outputChatBox("Your car has been despawned!", p, 0, 255, 0) local nups = toJSON(getVehicleUpgrades(nveh)) destroyElement(nveh) executeSQLQuery("UPDATE vehicles SET upgrades=? WHERE accName=? AND id=?", nups, accName, carID) setElementData (p, "System.Vehicle", false) end end addEvent("spawnDespawnCar", true) addEventHandler("spawnDespawnCar", root, spawnDespawnCar) I'm having problems saving and getting some upgrades of the car to SQLite, I'm quite new to JSON functions and I guess that I'm not getting it right. Edited June 9, 2015 by Guest Link to comment
Anubhav Posted June 9, 2015 Share Posted June 9, 2015 It's since you are inserting the upgrades in the health table and getting it from upgrades table. Link to comment
Baseplate Posted June 9, 2015 Author Share Posted June 9, 2015 I didn't post the full code, it is right in my actual code. Link to comment
Walid Posted June 9, 2015 Share Posted June 9, 2015 I didn't post the full code, it is right in my actual code. Try this function spawnDespawnCar(carName) local acc = getPlayerAccount(source) if acc and not isGuestAccount(acc) then local accName = getAccountName(acc) local carID = getVehicleModelFromName(carName) local result = executeSQLQuery("SELECT * FROM vehicles WHERE accName=? and ID=?", tostring(accName), tonumber(carID)) if (not getElementData (source,"System.Vehicle")) then local veh = createVehicle(...) local upgrades = fromJSON(result[1]["upgrades"]) for k, i in ipairs(upgrades) do addVehicleUpgrade( veh, i ) end setElementData (source, "System.Vehicle", veh) outputChatBox("Your car has been spawned!",source, 0, 255, 0) else local nveh = getElementData (source, "System.Vehicle") outputChatBox("Your car has been despawned!", source, 0, 255, 0) local nups = toJSON(getVehicleUpgrades(nveh)) executeSQLQuery("UPDATE vehicles SET upgrades=? WHERE accName=? AND id=?", nups, accName, carID) setElementData (source, "System.Vehicle", false) destroyElement(nveh) end end end addEvent("spawnDespawnCar", true) addEventHandler("spawnDespawnCar", root, spawnDespawnCar) Link to comment
Baseplate Posted June 9, 2015 Author Share Posted June 9, 2015 [2015-06-09 12:43:17] ERROR: [server]\vehicles\server.lua:63: bad argument #1 to 'ipairs' (table expected, got nil) Link to comment
Walid Posted June 9, 2015 Share Posted June 9, 2015 (edited) the code is working fine check your database if there is upgrades column and check if there is a result or not. Edited June 10, 2015 by Guest Link to comment
Baseplate Posted June 9, 2015 Author Share Posted June 9, 2015 executeSQLQuery("INSERT INTO vehUpgrades (accName, id, upgrades)", accName, carID, "empty") I'm not sure about the "empty" value, with what should I replace it? (I wanted to make an independant table for the upgrades, so I've changed it a bit). Link to comment
Baseplate Posted June 10, 2015 Author Share Posted June 10, 2015 I'll bump this as I still need some help. Link to comment
Anubhav Posted June 16, 2015 Share Posted June 16, 2015 Well, if you still need help. I'll tell you a BIG MISTAKE YOU DID, A REALLY BIG ONE. SQLite is case sensitive. local result = executeSQLQuery("SELECT * FROM vehicles WHERE accName=? and ID=?", tostring(accName), tonumber(carID)) executeSQLQuery("UPDATE vehicles SET upgrades=? WHERE accName=? AND id=?", nups, accName, carID) well if you see this, do you observe id and ID? Please show me your CREATE query, then I'll tell you the which should solve this problem. Link to comment
Baseplate Posted June 16, 2015 Author Share Posted June 16, 2015 Yes, I still need help lmao.. function startTables() executeSQLQuery("CREATE TABLE IF NOT EXISTS vehicles (accName TEXT, id NUM, price NUM, health NUM, r NUM, g NUM, b NUM, x NUM, y NUM, z NUM, locked TEXT)") executeSQLQuery("CREATE TABLE IF NOT EXISTS vehUpgrades (accName TEXT, id NUM, upgrades TEXT)") end addEventHandler("onResourceStart", resourceRoot, startTables) Link to comment
bradio10 Posted June 16, 2015 Share Posted June 16, 2015 Have you tried doing: local upgrades = fromJSON(result[1].upgrades) or local upgrades = fromJSON(result.upgrades) instead of what you currently have which is local upgrades = fromJSON(result[1]["upgrades"]) Not sure if it will make any difference/fix the issue, but I've never seen the way you do it before. Link to comment
Baseplate Posted June 16, 2015 Author Share Posted June 16, 2015 It shouldn't make any difference, I always do it that way and never failed me. Link to comment
Markeloff Posted June 16, 2015 Share Posted June 16, 2015 I hope this help you : You can use a string to save instead of a toJSON tables. function startTables() executeSQLQuery("CREATE TABLE IF NOT EXISTS vehicles (accName TEXT, id NUM, price NUM, health NUM, r NUM, g NUM, b NUM, x NUM, y NUM, z NUM, locked TEXT, vehUpgrades STRING)") addEventHandler("onResourceStart", resourceRoot, startTables) -- updating upgrade : local upgrades = getVehicleUpgrades(veh) local save = table.concat(upgrades, ";") executeSQLQuery("UPDATE vehicles SET vehUpgrades=? WHERE accName=? AND id=?", save, accName, carID) -- adding upgrades : querySQL = executeSQLQuery("SELECT * FROM vehicles WHERE accName=? AND id=?", accName, carID) local save = querySQL[1]["vehUpgrades"] local upgrades = split(save, string.byte(";")) for index, v in pairs(upgrades) do addVehicleUpgrade(veh, v) end Link to comment
Baseplate Posted June 16, 2015 Author Share Posted June 16, 2015 WARNING: [server]\vehicles\server.lua:59: Bad argument @ 'split' [Expected string at argument 1, got nil] [2015-06-16 15:15:58] ERROR: [server]\vehicles\server.lua:60: bad argument #1 to 'pairs' (table expected, got number) Link to comment
Anubhav Posted June 17, 2015 Share Posted June 17, 2015 Yes, I still need help lmao.. function startTables() executeSQLQuery("CREATE TABLE IF NOT EXISTS vehicles (accName TEXT, id NUM, price NUM, health NUM, r NUM, g NUM, b NUM, x NUM, y NUM, z NUM, locked TEXT)") executeSQLQuery("CREATE TABLE IF NOT EXISTS vehUpgrades (accName TEXT, id NUM, upgrades TEXT)") end addEventHandler("onResourceStart", resourceRoot, startTables) Well I got this now! local result = executeSQLQuery("SELECT * FROM vehicles WHERE accName=? and ID=?", tostring(accName), tonumber(carID)) to local result = executeSQLQuery("SELECT * FROM vehicles WHERE accName=? and id=?", tostring(accName), tonumber(carID)) Link to comment
Baseplate Posted June 17, 2015 Author Share Posted June 17, 2015 Anubhav that wasn't the issue, Markeloff was right and I made a slight mistake, fixed and works now. Thanks everyone! Link to comment
Markeloff Posted June 17, 2015 Share Posted June 17, 2015 Anubhav that wasn't the issue, Markeloff was right and I made a slight mistake, fixed and works now.Thanks everyone! You're welcome, anytime 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