views:

304

answers:

1

How do I check if an sqlite query returned anything before I stop looping through results.

//see if there are results and do something if not
while(sqlite3_step(selectstmt) == SQLITE_ROW) 
{ /*process on reults */}
+2  A: 

I think I'm missing something in your question, if it returns SQLITE_ROW then it has a row ready for reading.

The API is at http://www.sqlite.org/capi3ref.html#sqlite3_step

You can access that data through the column functions:

http://www.sqlite.org/capi3ref.html#sqlite3_column_blob

So you're basically doing the correct thing, while sqlite3_step return SQLITE_ROW you still have data to access.

// Clarification

SQLite fetches and gives you the rows one at a time by using sqlite3_step, calling it won't retrieve the whole resultset. So you won't know until you call step whether you're going to get data.

I've had to use SQLite quite a bit when I worked on embedded devices, but always through a decent DAL. I'm not a huge fan of it's API, but it is lightweight I guess.

Andrew Barrett
Thanks. I thought by calling sqlite3_step(selectstmt) I execute the query. I don't want to execute twice. Once to check SQLITE_ROW and another to loop through results. Or is that the only way without creating an array to hold/cache the results?
4thSpace
I've tried to clarify a bit there.
Andrew Barrett