I’m doing some work with Corona and SQLite. Having used all the examples on this site and ones I can find on the internet I cannot find an example to select a single record / row (i.e. select name,age,title from tbuser where id=1 ) ?
Could anyone help with an example or point me in the direction of another related forum? [import]uid: 10893 topic_id: 3520 reply_id: 303520[/import]
Often you only need the first row that a SELECT statement returns. For example counting the number of rows in a table and so on. Using db:rows() and related for such a task is a mess, instead you should use db:first_row() and related for this task:
row = db:first_irow(“SELECT count(*) FROM test”)
print(row[1])
row = db:first_row(“SELECT count(*) AS count FROM test”)
print(row.count)
count = db:first_cols(“SELECT count(*) FROM test”)
print(count)
There doesn’t exist a caveat in db:first_cols() like in db:cols(), instead always exactly the columns of the first row from the SELECT statement are returned.
[import]uid: 10893 topic_id: 3520 reply_id: 10696[/import]
ok I just got round to testing this and it does work - HELP!
stmt = db:prepare(“SELECT Title,Site,Due FROM tbHelpDeskJobs where id=”…id)
row = stmt:first_row()
DataObjects.lua:91: attempt to call method ‘first_row’ (a nil value)
stack traceback:
[C]: in function ‘first_row’… [import]uid: 10893 topic_id: 3520 reply_id: 11155[/import]
I’m having the exact same problem, and it is driving me insane. If I execute the following in SQLiteManager it returns the number that is in the database:
SELECT score FROM scores WHERE portal = '1' and level = '1'
Returns “2185”.
But when I run the following code in Corona:
[code]oldScore = db:exec( “SELECT score FROM scores WHERE portal = ‘1’ and level = ‘1’” )
print( oldScore )[/code]
Returns “0”
What am I doing wrong? [import]uid: 7227 topic_id: 3520 reply_id: 16082[/import]
don’t know if this is what your after but,
This will get you a single entry
dbase is table name, lim2 is how many records you want ( so that would be 1)
and lim is where to start ie what row
doing the statement first allows you to use vars, couldn’t get it to work directly
simple3 = 'SELECT \* FROM dbase LIMIT '..lim2..' OFFSET '..lim
for col1,col2 in db2:urows( simple3 ) do
print(col1)
print(col2
end
db:exec runs sql code, and returns 0 if it succeeds.
db:nrows is an iterator that will return a table with named columns for each item.
so for one item you’d do something like this.
[lua]for a in db:nrows(‘SELECT timestamp FROM table WHERE something= ‘1’ LIMIT ‘1’’)
do
output = a[timestamp]
end[/lua] [import]uid: 3 topic_id: 3520 reply_id: 16159[/import]
Sean, remember that SQlite restart bug we spoke about? Seems to be limited to the simulator, not been able to replicate it on device yet. [import]uid: 13089 topic_id: 3520 reply_id: 16504[/import]
Is there any other way that…without for loop we can get the single record from sql statement?
Thanks,
Krunal [import]uid: 33757 topic_id: 3520 reply_id: 22929[/import]
What have you tried that hasn’t worked? Maybe it can serve as a starting point for others to try out. [import]uid: 8045 topic_id: 3520 reply_id: 22931[/import]
for row in db:nrows(“SELECT * FROM Employee where Id =”…id) do
detailScreenText.text = “ID = “… row.Id…”\nName :”…row.Name
end
Its working fine for me.
but i want to do somethings like,
row = db:[function name](“SELECT * FROM Employee where Id =”…id)
@ seanh
Sean I tried your for loop but I can not get this to work:
local db = getDatabase()
local stmt = db:prepare("SELECT count(id) FROM ? LIMIT 1")
stmt:bind( 1, inTable.tableName )
for row in stmt:rows() do
print(row[1])
end
db:close()
It is giving me the error:
database.lua:60: attempt to index global 'stmt' (a nil value)
Line 60 maps to line six above.
Any idea how stmt gets to nil? 2 lines above I don’t get an error using stmt for the bind [import]uid: 12090 topic_id: 3520 reply_id: 42822[/import]
i really don’t know how that should work just with a single row
row = db:first_row(“SELECT count(*) AS count FROM test”)
print(row.count)
just a single command, (no loop) and a simple result (amount of rows)
but it does not work…i also looked into the sqlite documentation,
there not even a command “first_row” … so what ?
till now i still have to work with
for row in mdfunc.db:nrows(“SELECT count(*) AS count FROM data LIMIT 1”) do
print (row.count)
end
till now its still the fastest solution I found
chris
[import]uid: 4795 topic_id: 3520 reply_id: 54765[/import]
Hi all,
Any solutions solved fro this Sql nil value problem.
Me i have been using the WHERE
for row in db:nrows(“SELECT * FROM database WHERE Num=”…Var) do
Where Var is a Random number derived from math.random.
Now i am having a problem where a nil value is appearing SOMETIMES - have checked the database and the field has a value.
Only thing i can think of is that the result gets passed through a couple of variables (a = b, c = b, Display.newText(c, …) - just thought i would get this question out before i try reducing the transfers, or start coding a work around test loop ie if Nil then redo database call.
Thanks
tone
[import]uid: 199068 topic_id: 3520 reply_id: 134582[/import]
Hi all,
Any solutions solved fro this Sql nil value problem.
Me i have been using the WHERE
for row in db:nrows(“SELECT * FROM database WHERE Num=”…Var) do
Where Var is a Random number derived from math.random.
Now i am having a problem where a nil value is appearing SOMETIMES - have checked the database and the field has a value.
Only thing i can think of is that the result gets passed through a couple of variables (a = b, c = b, Display.newText(c, …) - just thought i would get this question out before i try reducing the transfers, or start coding a work around test loop ie if Nil then redo database call.
Thanks
tone
[import]uid: 199068 topic_id: 3520 reply_id: 134582[/import]