FlyingSpoon Posted February 11, 2018 Share Posted February 11, 2018 So I want to get some data from my database and insert it into a table or make it into a table so I can run a few things e.g. local general = {} addEventHandler("onResourceStart", root, function() local q = dbQuery(connection, "SELECT * FROM table") local r = dbPoll(q, -1) general = { {r["first"], r["second"]} } end) function run() for i, v in ipairs(general) do outputChatBox(v[1].." - "..v[2], player) end addCommandHandler("runme", run) How would I go about doing this? Link to comment
NeXuS™ Posted February 11, 2018 Share Posted February 11, 2018 (edited) addEventHandler("onResourceStart", root, function() local q = dbQuery(function(nrQ) local rQ = dbPoll(nrQ, 0) for i, k in pairs(rQ) do table.insert(general, {k["first"], k["second"]}) end end, connection, "SELECT * FROM table") end) I'd never use dbPoll with -1 timeout, as it can cause freezes. dbPoll returns a table of all data, { { ["first"] = "first", ["second"] = "second" }, { ... }, { ... } } so you have to loop through it. Edited February 11, 2018 by NeXuS™ Link to comment
FlyingSpoon Posted February 12, 2018 Author Share Posted February 12, 2018 Worked, but why is the loop as a variable? local q = dbQuery(function(nrQ)) And what is nrQ? Link to comment
FlyingSpoon Posted February 12, 2018 Author Share Posted February 12, 2018 Oh I get the dbQuery part now, handle dbQuery ( [ function callbackFunction, [ table callbackArguments, ] ] element databaseConnection, string query [, var param1 [, var param2 ...]] ) That you can enter a function before the connection, I didn't know that. Is nrQ the query? Link to comment
NeXuS™ Posted February 12, 2018 Share Posted February 12, 2018 Yes, nrQ is the query. 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