lua-users home
lua-l archive

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


	Hi Jan

	I'd like to add that this behavior is detailed in the docs:

"cur:fetch([table[,modestring]])
    Retrieves the next row of results.
    If fetch is called without parameters, the results will be returned
    directly to the caller. If fetch is called with a table, the
    results will be copied into the table and the changed table will
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    be returned. In this case, an optional modestring parameter can be
    used. It is just a string indicating how the resulting table should
    be constructed."

	This is intentional: one could have many rows to retrieve
and would not like to build and destroy tables in every iteration.
This could cause an unnecessary waste of time by the garbage collector.
OTOH, if you want to store the whole result set, you have to provide
different tables each time you call `fetch'.

	Regards,
		Tomás

On Sat, 27 Jan 2007, [HZ-GB-2312] Jan Sch~{(9~}tze wrote:

> Thanks rici!
> 
> So it wasn't an issue with references, more an issue with definition of
> cur:fetch.
> 
> Thank you very much.
> 
> Kind regards,
> Jan (DracoBlue)
> 
> Rici Lake wrote:
> >
> > On 27-Jan-07, at 11:29 AM, Jan Sch~{(9~}tze wrote:
> >
> >> This is the source I use.
> >>
> >> It uses a luasql mysql connection in mysql.con.
> >>
> >> function mysql_query(query,connection)
> >> if (connection==nil) then connection=mysql.con end
> >> return connection:execute(query)
> >> end
> >> function mysql_get(query,connection)
> 
> >> local rows={}
> >> local cur = assert (mysql_query(query,connection))
> >> local row = cur:fetch ({}, "a")
> >> while (row) do
> >> local row2={}
> >> for key,value in pairs(row) do
> >> row2[key]=value
> >> end
> >> table.insert(rows,row2)
> >> row = cur:fetch (row, "a")
> >
> > I think this is your problem. You're telling cur:fetch to modify the
> > table row, so it returns the same table each time, modifying its
> > values. You should probably change it to:
> > row = cur:fetch ({}, "a")
> > as in your initial calll in order to get a new table each time.
> >
> >> end
> >> return rows
> >> end
> >>
> >> Since there are 3 elements in the database, (which are correctly
> >> printed if I add a print_r(row) right after the while beginning.
> >>
> >> But if I replace
> >> while (row) do
> >> local row2={}
> >> for key,value in pairs(row) do
> >> row2[key]=value
> >> end
> >> table.insert(rows,row2)
> >> row = cur:fetch (row, "a")
> >> end
> >>
> >> with:
> >> while (row) do
> >> table.insert(rows,row)
> >> row = cur:fetch (row, "a")
> >> end
> >>>
> >> it won't work and raises errors like I described before.
> >>
> >> Kind regards,
> >> Jan (DracoBlue)
> >>
> >> David Given schrieb:
> >>> -----BEGIN PGP SIGNED MESSAGE-----
> >>> Hash: SHA1
> >>>
> >>> Jan Sch~{(9~}tze wrote:
> >>> [...]
> >>>
> >>>> The following pseudo code won't add every 3 rows to the table rows,
> >>>> because row is a variable so rows would contain one item, and 3
> >>>> pointers
> >>>> to it.
> >>>> rows={}
> >>>> local row = fetchRow()
> >>>> while (row) do
> >>>> table.insert(rows,row)
> >>>> rrow = fetchRow()
> >>>> end
> >>>>
> >>>
> >>> The only way you'd end up with the same item three times is if
> >>> fetchRow()
> >>> were somehow returning the same item every time. It's not reusing
> >>> the same
> >>> table to return each item, is it --- can we see the implementation?
> >>>
> >>> - --
> >>> ~{)0)$)$~} ~{#d#g#@#c#o#w#l#a#r#k#.#c#o#m~} ~{)$)$)$~} http://www.cowlark.com
> >>> ~{)$)$)$)$)$)$)$)$)$)$)$)$)$)$)$)$)$)$)$~}
> >>> ~{)&~} "There is nothing in the world so dangerous --- and I mean
> >>> *nothing*  ---
> >>> ~{)&~} as a children's story that happens to be true." --- Master Li Kao,
> >>> _The
> >>> ~{)&~} Bridge of Birds_
> >>> -----BEGIN PGP SIGNATURE-----
> >>> Version: GnuPG v1.4.3 (GNU/Linux)
> >>> Comment: Using GnuPG with MultiZilla - http://enigmail.mozdev.org
> >>>
> >>> iD8DBQFFu3Rnf9E0noFvlzgRAqwyAKCcqhtJ33uE2rgKWcZ0pWN3cnygDACfeJ2/
> >>> ArP4T0P73WTQeQ7/65KfEmU=
> >>> =5ego
> >>> -----END PGP SIGNATURE-----
> >>>
> >>>
> >>>
> >
> >
> >
>