How to bind dynamic NSNumber Object to SQLite query?
I'm wondering how to bind an NSNumber o开发者_JS百科bject into a sqlite3 query:
const char *sql = "select name from myTable where section_id=1 order by name ASC";
I have a NSNumber object called 'nmbr' and want to assign its value to the section id..
Try this:
const char *sql = "select name from myTable where section_id=? order by name ASC";
sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL) == SQLITE_OK;
NSNumber sid = [NSNumber numberWithInt:1];
sqlite3_bind_int (stmt, 1, [sid intValue]);
sqlite3_step(stmt);
sqlite3_reset(stmt);
Remember: you should deal with error return codes. For the sake of simplicity I am ignoring them here.
精彩评论