JeViCo Posted August 3, 2018 Share Posted August 3, 2018 Hello everyone! I have a little problem with fromJSON. Saving with toJSON works fine but loading with fromJSON causes warnings and i can't get value from database (i keep getting nil) saving(everything ok here) local result = dbPoll(dbQuery(db, "SELECT * FROM sticks WHERE ID = ? AND model = ?", id, getElementModel(veh)), -1) if type(result) == "table" then if #result ~= 0 then dbExec(db, "UPDATE sticks SET stick = ?, model = ? WHERE ID = ?", toJSON(sti), getElementModel(veh), id) else print(1111) --print(sti[1][1]) --if not sti then sti = {} end dbExec(db, "INSERT INTO sticks VALUES(?, ?, ?)", id, toJSON(sti), getElementModel(veh)) end end loading (a problem right here): local r1 = dbPoll(dbQuery(db, "SELECT * FROM sticks WHERE ID = ? AND model = ?", id, getElementModel(source)), -1) if type(r1) == "table" and #r1 ~= 0 then setElementData(source,"stick",fromJSON(r1)) --print(r1[1]) else setElementData(source,"stick",{}) end end appearence of value in database: what i get: Help! Link to comment
SycroX Posted August 3, 2018 Share Posted August 3, 2018 replace line 3 in the problem with this setElementData(source, "stick", fromJSON(r1[1]["stick"])) Link to comment
JeViCo Posted August 3, 2018 Author Share Posted August 3, 2018 6 minutes ago, #َxLysandeR said: replace line 3 in the problem with this setElementData(source, "stick", fromJSON(r1[1]["stick"])) well\ it didn't helped me ["stick"] is spare right here. r1[1] - table t1[1][1] - nil WTF r[1] must be {{value1,value2},{value3,value4} } if i remove toJSON and fromJSON i will get an empty cell in my database Link to comment
SycroX Posted August 3, 2018 Share Posted August 3, 2018 32 minutes ago, Juuve said: well\ it didn't helped me ["stick"] is spare right here. r1[1] - table t1[1][1] - nil WTF r[1] must be {{value1,value2},{value3,value4} } if i remove toJSON and fromJSON i will get an empty cell in my database i don't have any time to explain it for you but did u try it ? 1 Link to comment
JeViCo Posted August 3, 2018 Author Share Posted August 3, 2018 1 hour ago, #َxLysandeR said: i don't have any time to explain it for you but did u try it ? I'm so sorry. I used wrong event. I restarted resource and it works brilliant. Thanks! Link to comment
itHyperoX Posted August 4, 2018 Share Posted August 4, 2018 (edited) If you using this: - If you have a high player count, it's can cause some lag. local r1 = dbPoll(dbQuery(db, "SELECT * FROM sticks WHERE ID = ? AND model = ?", id, getElementModel(source)), -1) -- I prefer dbQuery(function(id, query) local result, row, _ = dbPoll(query, -1) -- here you can use for k, value in ipairs(result) do -- or simple sticker = result[1] end, {id}, db, "SELECT * FROM stickers WHERE ID=? AND model=?", id, getElementModel(source)) Edited August 4, 2018 by TheMOG 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