Jump to content

fromJSON + tables problem


JeViCo

Recommended Posts

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:

screenshot_1.png

what i get:

mta-screen_2018-08-03_17-16-06.jpg

Help!

Link to comment
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 O.o

["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
32 minutes ago, Juuve said:

well\ it didn't helped me O.o

["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 ? 

 

  • Thanks 1
Link to comment

If you using 

this:

- If you have a high player count, it's can cause some lag.

  1. 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 by TheMOG
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...