Recently I tried to build a dynamic SQL Statement within XCode and it proved more difficult than expected. As the parsing function used my SQLite enjoys getting a Const Char data type, building that from a NSString was not a simple matter. After some investigation my first attempt came up with using the
int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); function while passing a sql statement like this
"Select * from table1 where name=?". The idea was that this would replace the "?" with the value I passed through, however it never seemed to bring back any records. SQLite does not make it easy to see the actual statement (Oh the joys of MS SQL Profiler!), so I could not see what was going wrong.
An alternative I came up with was to use the following:
NSString *sql1 = [[NSString alloc] initWithFormat:@"Select * FROM Table1 where name='%@'", paramName];
NSString *sql = [NSString stringWithUTF8String:[sql1 UTF8String]];
sqlite3_stmt *selectstmt = nil;
if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &selectstmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW) { ....... }
}
This worked fine, however if someone has an alternative, please let me know.
No comments:
Post a Comment