Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Selecting from a database record using variables for field references
Started by richard11 May 12 2019 12:52 PM

- - - - -
3 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

richard11

[GLOBAL: userInfoPane.html]
richard11
  • Contributor

  • 464 posts
  • Corona SDK

Is this possible in Lua?

 

In other languages, I'm used to fetching a record as an array/table and being able to just grab the appropriate field from that set using variables. Something like this:

$field1 = "fldSomething";
$field2 = "fldSomethingElse";
$record = dbFetch(" SELECT * FROM `tblTable` WHERE fldFoo = 'Bah' ");

echo $field1 . " = " . $record[$field1];
echo $field2 . " = " . $record[$field2];

But in Lua, records don't seem to be returned as assoc tables and I'm struggling to find a workaround?

 

I.e. say I'm iterating records like this:

for result in db:nrows( [[SELECT * FROM `tblTable` WHERE fldFoo = "Bah";]] ) do
    print("fldSomething = " .. result.fldSomething )
end

How would I amend that print statement if fldSomething isn't a hard-coded name?

local field1 = "fldSomething"

for result in db:nrows( [[SELECT * FROM `tblTable` WHERE fldFoo = "Bah";]] ) do
    -- I obviously can't just do result.field1 here, because field1 isn't a property of result...
    print(field1 .. " = " .. result.field1 )
end

Cheers.



[TOPIC: post.html]
#2

richard11

[GLOBAL: userInfoPane.html]
richard11
  • Contributor

  • 464 posts
  • Corona SDK

Scrap this, I'm being thick. Turns out result can be read as a table too, so result[field1] works just fine.

 

Clearly it's past my bedtime!



[TOPIC: post.html]
#3

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 517 posts
  • Corona SDK

Richard,

 

As busy as and productive as you are, I'm surprised to learn that you have a bedtime.  ;)



[TOPIC: post.html]
#4

richard11

[GLOBAL: userInfoPane.html]
richard11
  • Contributor

  • 464 posts
  • Corona SDK

Ha. Mathematically I'm 33, but both physically and mentally I'm well over 100. It's difficult to stay awake after 9pm when you're over 100...
  • sporkfin likes this


[topic_controls]
[/topic_controls]