objective c create dynamic query to pass to sqlite3_prepare_v2
I should create a query to pass to sqlite3_prepare_v2.
The query must be dynamic, and must al开发者_StackOverflow社区low me to retrieve all records that, in at least one field, containing at least one of the search filters.
For example: -fields are f1, f2 -filters are SF1, SF2
I need to create dynamically: where f1 like '%SF1%' or f1 like '%sf2%' or f2 like '%SF1%' or f2 like '%sf2%'
Can you show the code that does what I described?
Thanks in advance for your response.
selectStmt = nil;
NSString strParam1 = @"%SF1%";
NSString strParam2 = @"%SF2%";
const char *selectSql;
//prepare select statement
if(sqlite3_prepare_v2(database, sqlStatement, -1, &selectStmt, NULL) == SQLITE_OK)
{
//Bind your parameter
sqlite3_bind_text(selectStmt, 1, [strParam1 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(selectStmt, 2, [strParam2 UTF8String], -1, SQLITE_TRANSIENT);
while(sqlite3_step(selectStmt) == SQLITE_ROW)
{
//Read your values from database
NSString *f1Value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStmt, 0)];
NSString *f2Value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStmt, 1)];
}
}
精彩评论