lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Sep 8, 2016 at 9:19 PM, Doug Currie <doug.currie@gmail.com> wrote:
On Thu, Sep 8, 2016 at 8:26 PM, Leinen, Rick <RLeinen@leviton.com> wrote:

 I see how you can print the values in a database table using db:nrows in a For statement, but what if I want just a single entry?


Using a for loop with db:nrows takes care of the prepare, step, and finalize that are necessary. So, it is usually the easiest approach. Why are you not interested in all the results of the query? Perhaps you should use 'limit 1' if you only want one result. 

 This also works, if you really need just the first row...

> fn,vm = db:nrows 'select * from numbers where num1=1''
> row = fn(vm)
> row.str
ABC
> vm:finalize()
0

or

> for row in db:nrows'select * from numbers where num1 = 1' do print(row.str); break end
ABC

but I still prefer

> for row in db:nrows'select * from numbers where num1 = 1' do print(row.str) end
ABC

e