views:

32

answers:

1
+2  Q: 

debugging sqlite

is there a way to see what the resulting 'showStatement' is after sqlite3_prepare_v2 and sqlite3_bind_xxx ?

Running this query :

SELECT * FROM shows, locations 
WHERE (shows.day_id = 1) 
  AND (shows.id IN (6,7,15,19,23,66)) 
  AND (shows.location_id = locations.id)
ORDER by locations.sort_order

runs perfect in SQLite Manager and in code when I enter it EXACTLY like that. If I however do parameter substitution the querey returns no results...

if (sqlite3_open([databasePath UTF8String],&showsDatabase) == SQLITE_OK){
    const char *sqlStatement = "SELECT * FROM shows, locations WHERE (shows.day_id = ?) AND (shows.id IN (?)) AND (shows.location_id = locations.id) ORDER by locations.sort_order";
        sqlite3_stmt *showStatement;
        if(sqlite3_prepare_v2(showsDatabase, sqlStatement, -1, &showStatement, NULL) == SQLITE_OK) {
            sqlite3_bind_int(showStatement, 1, forDay);
            sqlite3_bind_text(showStatement, 2, allFavorites,-1,NULL);
            int error = sqlite3_step(showStatement);
            while(sqlite3_step(showStatement) == SQLITE_ROW) {

...

The problem must lie in the IN (6,7...) part, without that it works perfect.

My debugger shows me that forDay = 1 and that allFavorites = 6,7,15,19,23,66 but the error = 101 = sqlite3_step() has finished executing = no lines found

being able to see the 'showStatement' variable in one way or another would solve the problem, however the debugger doesn't give that info

+1  A: 

You can't bind IN (?) with an array.

You need to write shows.id IN (?, ?, ?, ?, ?, ?) and bind each parameter separately. The number of question marks also has to match the number of parameters so you might need to construct the query dynamically if the number can vary.

Mark Byers
Thank you very much, that solved the problem. However for the future is there any way I can 'see' via the debugger what is in my 'showStatement' after the binding ? (would be handy to debug)
Glenn
@Glenn: I don't know sorry. :( But I can guess that it probably looks like this: `IN ('6,7,15,19,23,66')`, i.e. a single string, not a list of integers.
Mark Byers