Jump to content

[Help]dbpoll SQlite table


HustraDev

Recommended Posts

Hi 2 all , i'm working on script i almost finish it but i have a problem that is i want to insert database row's in GUI gridlist

well i want to poll all rows by account name like this >> Server Side

function()
	local rows = dbQuery( connection, "SELECT * FROM `VList` WHERE `account`=?",getAccountName(getPlayerAccount(client)))
	local result = dbPoll( rows, -3 )
	if result and type( result ) == 'table' then 
    local sID = result[1][ 'id' ] 
	local sName = result[2][ 'name' ]
	local sPrice = result[3][ 'price' ]
	triggerClientEvent(source,"SendRows",client,sID,sName,sPrice)
	end

Client :

   function(sID,sName,sPrice)
   
     local row = guiGridListAddRow ( PlayerVehicleList ) 
     guiGridListSetItemText ( PlayerVehicleList, row, 1, sID, false, false ) 
     guiGridListSetItemText ( PlayerVehicleList, row, 2, sName, false, false ) 
	 guiGridListSetItemText ( PlayerVehicleList, row, 3, sPrice, false, false ) 

   
  end

but the problem is if the database contains more the 1 rows have the same account name it well insert only a 1 row in gui gridlist 

:  [ database.db ]

----------------------------------------

account | id | name | price |

koko00 | 12 | hustra | 2000 |

koko00 | 14 | dev | 5000 |

----------------------------------------

Gui gridlist

-----------------------

id | name | price

12 | dev | 2000

-----------------------

i hope anyone can help me ... sorry for bad english

Link to comment
28 minutes ago, Gravestone said:

As pa3ck said, use 'for' statement for sending each row to the client side.

And this, 'client' is a clientsided pre defined variable and I don't anywhere client being defined in your function.

can you explain to how to use for statement to get every rows that contains account name?

Link to comment
-- server --
function sendRowsToClient()
	local result = dbPoll(dbQuery( connection, "SELECT * FROM `VList`", -1))
	if result and type( result ) == 'table' then 
		for i, v in ipairs(result) do
		triggerClientEvent(source, "onClientDataReceived", source, v.id, v.name, v.price)
		end
	end
end	

-- client --
addEvent("onClientDataReceived", true)
addEventHandler("onClientDataReceived", root,
function(sID,sName,sPrice)
	local row = guiGridListAddRow ( PlayerVehicleList ) 
	guiGridListSetItemText ( PlayerVehicleList, row, 1, sID, false, false ) 
	guiGridListSetItemText ( PlayerVehicleList, row, 2, sName, false, false ) 
	guiGridListSetItemText ( PlayerVehicleList, row, 3, sPrice, false, false ) 
	end
)

Try this, not tested

Edited by Gravestone
  • Like 1
Link to comment
4 minutes ago, Gravestone said:

-- server --
function sendRowsToClient()
	local result = dbPoll(dbQuery( connection, "SELECT * FROM `VList`", -1))
	if result and type( result ) == 'table' then 
		for i, v in ipairs(result) do
		triggerClientEvent(source, "SendRows", source, v.id, v.name, v.price)
		end
	end
end	

-- client --
addEvent("onClientDataReceived", true)
addEventHandler("onClientDataReceived", root,
function(sID,sName,sPrice)
	local row = guiGridListAddRow ( PlayerVehicleList ) 
	guiGridListSetItemText ( PlayerVehicleList, row, 1, sID, false, false ) 
	guiGridListSetItemText ( PlayerVehicleList, row, 2, sName, false, false ) 
	guiGridListSetItemText ( PlayerVehicleList, row, 3, sPrice, false, false ) 
	end
)

Try this, not tested

thanks man i really really appreciate your help it works 100%

 

Link to comment
1 hour ago, Gravestone said:

-- server --function sendRowsToClient()	local result = dbPoll(dbQuery( connection, "SELECT * FROM `VList`", -1))	if result and type( result ) == 'table' then 		for i, v in ipairs(result) do		triggerClientEvent(source, "onClientDataReceived", source, v.id, v.name, v.price)		end	endend	-- client --
addEvent("onClientDataReceived", true)
addEventHandler("onClientDataReceived", root,
function(sID,sName,sPrice)
	local row = guiGridListAddRow ( PlayerVehicleList ) 
	guiGridListSetItemText ( PlayerVehicleList, row, 1, sID, false, false ) 
	guiGridListSetItemText ( PlayerVehicleList, row, 2, sName, false, false ) 
	guiGridListSetItemText ( PlayerVehicleList, row, 3, sPrice, false, false ) 
	end
)

Try this, not tested

 

It surely works, but it's terrible advice to use triggerClientEvent inside a loop for no reason. I mean, why can't you just send the whole table over and loop through it there?

 

-- server
function sendRowsToClient()
    local result = dbPoll(dbQuery( connection, "SELECT * FROM `VList`", -1))    
    if result and #result > 0 then 
        triggerClientEvent(source, "SendRows", source, result)
    end
end


-- client --
addEvent("SendRows", true)
addEventHandler("SendRows", root,
function(tableData)
    local data = tableData
    
    for k, v in ipairs(data) do
        local row = guiGridListAddRow ( PlayerVehicleList ) 
        guiGridListSetItemText ( PlayerVehicleList, row, 1, v["id"], false, false ) 
        guiGridListSetItemText ( PlayerVehicleList, row, 2, v["name"], false, false ) 
        guiGridListSetItemText ( PlayerVehicleList, row, 3, v["price"], false, false ) 
    end
    
end)


 

 

  • Like 1
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...