LuaSQLite3

Artifact Content
Login

Artifact 576e0bc9289bad870d34a7ac030d09e5931def31:

Ticket change [576e0bc928] - New ticket [b5ceb24111] Can't use the iterator from db:*rows as a variable. by anonymous 2019-09-15 15:30:11.
D 2019-09-15T15:30:11.289
J foundin 0.9.5-1
J icomment In\sLua,\san\siterator\sis\sa\sfunction\sthat\sreturns\snew\svalues\son\ssuccessive\scalls,\sand\sthe\sfunction\scan\sbe\spassed\saround\sas\sa\svariable.\sHowever,\sfor\ssome\sreason\sthis\sdoesn't\swork\swith\sthe\sdb:*rows\sfunctions\sof\slsqlite3:\r\n\r\n<pre>\r\nlocal\ssqlite3\s=\srequire("lsqlite3complete")\r\nlocal\sdb\s=\ssqlite3.open_memory()\r\n\r\nlocal\srows\s=\sdb:nrows("select\s11\sas\sqwe")\r\nfor\srow\sin\srows\sdo\r\n\s\s\s\sprint(row.qwe)\r\nend\r\n</pre>\r\n\r\nThis\sproduces\sthe\sfollowing\serror\smessage:\r\n\r\n<pre>\r\nlua:\ss.lua:5:\sbad\sargument\s#1\sto\s'for\siterator'\s(:sqlite3:vm\sexpected,\sgot\snil)\r\nstack\straceback:\r\n\t[C]:\sin\sfor\siterator\s'for\siterator'\r\n\ts.lua:6:\sin\smain\schunk\r\n\t[C]:\sin\s?\r\n</pre>\r\n\r\nThe\siteration\sonly\sworks\sif\sa\s*rows\sfunction\sis\scalled\sdirectly\sin\sa\s`for`\sstatement.\r\n\r\nThe\ssame\shappens\swith\sdb:rows,\sdb:nrows,\sand\sdb:urows\sfunctions\sand\scorresponding\sstmt:*rows\sfunctions\sfor\sprepared\sstatements.\sIt\saffects\sboth\slsqlite3complete\sand\slsqlite3\swith\sa\sdynamically\sloaded\ssqlite3\slib.\r\n\r\nMy\suse-case\sis\sthat\sI\suse\sFennel,\sa\sLisp\slanguage\son\stop\sof\sLua,\sand\sI\sneed\sto\spick\sjust\sthe\sfirst\srow\sfrom\sthe\sresults.\sAs\sa\sfunctional\slanguage,\sFennel\sdoesn't\shave\searly\sreturns\sor\s`break`\sstatements,\sbut\scould\seasily\scall\sthe\siterator\sjust\sonce.\sMore\sgenerally,\siterators\scould\sbe\sused\swith\shigher-order\sfunctions.\r\n\r\nVersions\sare\sas\sfollows:\r\n\r\nLSQLite3\s0.9.5-1,\sinstalled\swith\sLuarocks.<br\s/>\r\nLua\s5.3.5\sand\s5.1.5\sinstalled\swith\sHomebrew\s(haven't\stested\swith\sother\sversions).<br\s/>\r\nMacOSX\s10.13.6.\r\n\r\nFor\scomparison,\shere's\sLua\scode\screating\san\siterator\sas\sa\svar:\r\n\r\n<pre>\r\nfunction\screate_iterator(limit)\r\n\s\s\s\slocal\si\s=\s0\r\n\s\s\s\sreturn\sfunction()\r\n\s\s\s\s\s\s\s\si\s=\si\s+\s1\r\n\s\s\s\s\s\s\s\sif\si\s<=\slimit\sthen\sreturn\si\send\r\n\s\s\s\send\r\nend\r\n\r\niterator\s=\screate_iterator(10)\r\nfor\sx\sin\siterator\sdo\r\n\s\s\s\sprint(x)\r\nend\r\n</pre>
J login anonymous
J mimetype text/x-fossil-wiki
J private_contact ed1c74f3036cfa043d8e02a8b2806e9b50371cf2
J severity Minor
J status Open
J title Can't\suse\sthe\siterator\sfrom\sdb:*rows\sas\sa\svariable
J type Code_Defect
K b5ceb24111d7ecfdcf5bafefe82b61c60a7354d0
U anonymous
Z b7a967278fa4b2cf0ec6ed0e2125a465