lua-users home
lua-l archive

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


On Jun 3, 2010, at 2:54 AM, Luciano de Souza wrote:

> Searching a little bit more, I got this:
> 
> function last()
> 
> local GetRow, sqlite_vm = db:nrows('select id from contacts order by id desc limit 1')
> 
> local row = GetRow(sqlite_vm)
> 
> return row.id
> 
> end
> 
> db = sqlite3.open('ting.dbl')
> 
> print(last)
> 
> In other words, each time db:nrows is called, it returns two values: the first is a function that called, returns one row of results; the second is an internal value refering to the Sqlite Virtual Machine.
> 
> Calling GetRow, we obtain a table with the first row of result. Taking into consideration I used nrows, I got a table indexed by the names of the table, so I can call GetRow(sqlite_vm).id.


Your original code, below, is preferable because the GetRow function (as you call it above) is also responsible for finalizing and cleaning up the sqlite_vm. It does this automatically when used in the for-loop when it returns an empty row terminating the iteration.

An alternative is to use something like:

function last()
  local id
  db:exec('select id from contacts order by id desc limit 1',
          function(_,_,v,_) id = v[1]; return 0 end)
  return id
end

e


> ----- Original Message ----- From: "Luciano de Souza" <luchyanus@predialnet.com.br>
> To: "Lua list" <lua@bazar2.conectiva.com.br>
> Sent: Thursday, June 03, 2010 3:24 AM
> Subject: Re: Error with Lsqlite3
> 
> 
>> I got the result with this code:
>> 
>> db = sqlite3.open('ting.dbl')
>> 
>> for record in db:nrows('select id from contacts order by id desc limit 1') do
>> 
>> id = record.id
>> 
>> end
>> 
>> print(id)
>> 
>> It works, but it's unsuitable to iterate over a single row result. Actually, I can't understand functions returning functions. I came from Pascal world and this feature were not available.
>> 
>> Sometimes, I think: what is it really occuring behind the expression for record in db:nrows('select id from contacts order by id desc limit 1')?
>> 
>> When we do:
>> 
>> for k, v pairs(t) do
>> 
>> end
>> 
>> we can undestand that in each iteration, it occurs something like: t.k = v.
>> 
>> But which are the arguments of the iterating functions? Knwoing this, probabily we will able to call it one single time.
>> 
>> Ah! But it's a curiosity. My problem you have alredy solved. Thank you.
>> 
>> 
>> 
>> ----- Original Message ----- From: "Doug Currie" <doug.currie@gmail.com>
>> To: "Lua list" <lua@bazar2.conectiva.com.br>
>> Sent: Wednesday, June 02, 2010 9:08 PM
>> Subject: Re: Error with Lsqlite3
>> 
>> 
>> 
>> On Jun 2, 2010, at 7:47 PM, Luciano de Souza wrote:
>> 
>>> db = sqlite3.open('test.dbl')
>>> 
>>> print(db:last_insert_rowid())
>>> 
>>> The last rowid is undoubtedly 6, but I receive 0 as an answer.
>> 
>>> From http://www.sqlite.org/c3ref/last_insert_rowid.html
>> 
>>> This routine returns the rowid of the most recent successful INSERT into the database from the database connection in the first argument. If no successful INSERTs have ever occurred on that database connection, zero is returned.
>> 
>> Since db (the database connection) was just opened, 0 is the expected answer.
>> 
>> Perhaps you should try executing the query: select max(id) from contacts;
>> 
>> e
>> 
>> 
>> 
>