Hello all, I am currently creating a little loop that gets a value from a database. What I want to do is expand it so I can do this more than twice (what it currently does now). Any thoughts or suggestions?
My code is:
local previous={} local i = 1 local firstvalue= {} local secondvalue={} for z=1,2 do local randtest=math.random(1,4) previous[z]=randtest if z==1 then for row in db:nrows("SELECT \* FROM tableWHERE ID="..randtest)do firstvalue[z] = row.value1 secondvalue[z] = row.value2 end else while randtest==previous[z-1]do randtest=math.random(1,4) end for row in db:nrows("SELECT \* FROM table WHERE ID="..randtest)do firstvalue[z] = row.value1 secondvalue[z] = row.value2 end end end
Apologies about the lack of indentation
It would be helpful to know what you are trying to achieve. Do you want 2 random rows from the DB if so then do it in SQL
SELECT \* FROM table ORDER BY RANDOM() LIMIT 2
Sorry about not being specific enough. What I want to do is get x number of random rows from a DB and get the values from two columns within that row ie the table is set up like id value1 value2, where each row is unique. I want to store each value into two separate lua tables. Is that better?
This will fill your arrays with 2 random rows
local rowsToReturn = 2 local firstvalue, secondvalue = {}, {} for row in db:nrows("SELECT value1, value2 FROM table ORDER BY RANDOM() LIMIT "..rowsToReturn) do firstvalue[#firstvalue+1] = row.value1 secondvalue[#secondvalue+1] = row.value2 end
Thank you for that, it’s really simplified things. However it appears that sometines when I launch this it causes the simulator to freeze. Any idea what could be causing it?
No not really (without your full source code and DB it is almost impossible to call)… you will need to debug your code (lots of print statements help a lot) to figure out what is happening.
It would be helpful to know what you are trying to achieve. Do you want 2 random rows from the DB if so then do it in SQL
SELECT \* FROM table ORDER BY RANDOM() LIMIT 2
Sorry about not being specific enough. What I want to do is get x number of random rows from a DB and get the values from two columns within that row ie the table is set up like id value1 value2, where each row is unique. I want to store each value into two separate lua tables. Is that better?
This will fill your arrays with 2 random rows
local rowsToReturn = 2 local firstvalue, secondvalue = {}, {} for row in db:nrows("SELECT value1, value2 FROM table ORDER BY RANDOM() LIMIT "..rowsToReturn) do firstvalue[#firstvalue+1] = row.value1 secondvalue[#secondvalue+1] = row.value2 end
Thank you for that, it’s really simplified things. However it appears that sometines when I launch this it causes the simulator to freeze. Any idea what could be causing it?
No not really (without your full source code and DB it is almost impossible to call)… you will need to debug your code (lots of print statements help a lot) to figure out what is happening.