Hoq to use LIKE in SQLite query - iphone

I am trying to use LIKE in my code in following way:
-(void)getMatchedContacts{
if(sqlite3_open([[self GetDBPath] UTF8String], &database)== SQLITE_OK) {
NSMutableArray *arrFBContacts = [self getFBContacts];
if([arrFBContacts count]>0){
sqlite3_stmt *stmt;
for (MContact *mc in arrFBContacts){
const char *sql = "select count(*) from contacts where fname LIKE '?' and lname LIKE '?'";
if (sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) != SQLITE_OK) {
NSAssert1(0, #"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
else{
NSLog(#"%#",[NSString stringWithFormat:#"%#",mc.firstname]);
NSLog(#"%#",[NSString stringWithFormat:#"%#",mc.lastname]);
sqlite3_bind_text(stmt, 1, [[NSString stringWithFormat:#"%#",mc.firstname] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, [[NSString stringWithFormat:#"%#",mc.lastname] UTF8String], -1, SQLITE_TRANSIENT);
int count =0;
if(sqlite3_step(stmt) == SQLITE_ROW) {
count = sqlite3_column_int(stmt, 0);
}
sqlite3_reset(stmt);
if(count>0){
const char *sql1 = "update fbContacts set matched = 1 where id = ?";
if (sqlite3_prepare_v2(database, sql1, -1, &stmt, NULL) != SQLITE_OK) {
NSAssert1(0, #"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
else {
sqlite3_bind_int(stmt, 1, mc.fblocalcontactid);
sqlite3_step(stmt);
}
sqlite3_reset(stmt);
}
}
}
}
}
}
But I am not able to get any value for count variable from first SQL. What is wrong in y programme?

You can check my function as below: In that, like query is working properly.
-(NSMutableArray *)getCategorySearchNoDueDate:(int)catId:(NSString *)searchWord{
searchWord = [#"%" stringByAppendingFormat:#"%#%%",searchWord];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
oClsDBFunction = [[clsDBFunction alloc] init];
appDelegate = (WFAppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.result == SQLITE_OK) {
const char *sql;
NSString *sqlString = #"SELECT \"Project\" as objectType, \"0\" as tID, projectId as pID, \"0\" as refTID, referenceId as refPID, "
"title as pTitle, \"\" as tTitle, description as Desc, "
"startDate as StartDate, startTime as StartTime, "
"dueDate as DueDate, dueTime as DueTime, compDate as CompDate, compTime as CompTime, "
"priorityId as PriorityId, categoryId as CategoryId, "
"isCompleted as IsCompleted, status as Status, isDeleted as IsDeleted, "
"remark as Remark, repeatId as RepeatId, alertCount as AlertCount, \"0\" as TaskType, "
"(select count(*) from tblTask, tblReference "
"where tblTask.referenceId=tblReference.referenceId and tblReference.projectId = tblProject.projectId) as TotalTask, "
"(select count(*) from tblTask, tblReference "
"where tblTask.referenceId=tblReference.referenceId and tblReference.projectId = tblProject.projectId and tblTask.isCompleted=1) "
"as CompletedTask "
"FROM tblProject "
"WHERE dueDate='' and "
"IsCompleted=0 and CategoryId=? and (tblProject.title like ? or tblProject.description like ?) "
"UNION "
"SELECT \"Task\" as objectType, tblTask.taskId as tID, \"0\" as pID, tblTask.referenceId as refTID, \"0\" as refPID, "
"\"\" as pTitle, title as tTitle, description as Desc, "
"\"\" as StartDate, \"\" as StartTime, "
"dueDate as DueDate, dueTime as DueTime, compDate as CompDate, compTime as CompTime, "
"priorityId as PriorityId, categoryId as CategoryId, "
"isCompleted as IsCompleted, status as Status, isDeleted as IsDeleted, "
"remark as Remark, repeatId as RepeatId, alertCount as AlertCount, taskTypeId as TaskType, "
"\"0\" as TotalTask, "
"\"0\" as CompletedTask "
"FROM tblTask, tblReference "
"WHERE IsCompleted=0 and "
"dueDate='' and "
"CategoryId=? and "
"tblTask.referenceId = tblReference.referenceId and tblReference.projectId=0 and (tblTask.title like ? or tblTask.description like ?) "
"UNION "
"SELECT \"Task\" as objectType, tblTask.taskId as tID, tblProject.projectId as pID, tblTask.referenceId as refTID, \"0\" as refPID, "
"tblProject.title as pTitle, tblTask.title as tTitle, tblTask.description as Desc, "
"\"\" as StartDate, \"\" as StartTime, "
"tblTask.dueDate as DueDate, tblTask.dueTime as DueTime, tblTask.compDate as CompDate, tblTask.compTime as CompTime, "
"tblTask.priorityId as PriorityId, tblTask.categoryId as CategoryId, "
"tblTask.isCompleted as IsCompleted, tblTask.status as Status, tblTask.isDeleted as IsDeleted, "
"tblTask.remark as Remark, tblTask.repeatId as RepeatId, tblTask.alertCount as AlertCount, taskTypeId as TaskType, "
"\"0\" as TotalTask, "
"\"0\" as CompletedTask "
"FROM tblTask, tblReference, tblProject "
"WHERE tblTask.IsCompleted=0 and "
"tblTask.dueDate='' and "
"tblTask.CategoryId=? and "
"tblTask.referenceId = tblReference.referenceId and tblReference.projectId!=0 and "
"tblProject.projectId = tblReference.projectId and (tblTask.title like ? or tblTask.description like ?) "
"order by PriorityId DESC, IsCompleted";
sql = [sqlString UTF8String];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(appDelegate.database, sql, -1, &statement, NULL) == SQLITE_OK) {
NSString *strCatId = [NSString stringWithFormat:#"%d", catId];
sqlite3_bind_text(statement, 1, [strCatId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 4, [strCatId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 5, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 6, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 7, [strCatId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 8, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 9, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *strObjectType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
NSString *strTaskId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
NSString *strProjectId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
NSString *strRefTaskId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
NSString *strRefProjectId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 4)];
NSString *strProjectTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 5)];
NSString *strTaskTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 6)];
NSString *strDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 7)];
NSString *strStartDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 8)];
NSString *strStartTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 9)];
NSString *strDueDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 10)];
NSString *strDueTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 11)];
NSString *strCompDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 12)];
NSString *strCompTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 13)];
NSString *strPriorityId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 14)];
NSString *strCategoryId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 15)];
NSString *strIsCompleted = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 16)];
NSString *strStatus = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 17)];
NSString *strIsDeleted = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 18)];
NSString *strRemark = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 19)];
NSString *strRepeatId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 20)];
NSString *strAlertCount = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 21)];
NSString *strTaskType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 22)];
NSString *strTotalTask = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 23)];
NSString *strCompletedTask = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 24)];
oMainPage = [[MainPage alloc] init];
oMainPage.objectType = strObjectType;
oMainPage.tId = strTaskId;
oMainPage.pId = strProjectId;
oMainPage.refTId = strRefTaskId;
oMainPage.refPId = strRefProjectId;
oMainPage.pTitle = strProjectTitle;
oMainPage.tTitle = strTaskTitle;
oMainPage.strDesc = strDescription;
oMainPage.strStartDate = strStartDate;
oMainPage.strStartTime = strStartTime;
oMainPage.strDueDate = strDueDate;
oMainPage.strDueTime = strDueTime;
oMainPage.strCompDate = strCompDate;
oMainPage.strCompTime = strCompTime;
oMainPage.strPriority = strPriorityId;
oMainPage.strCategory = strCategoryId;
oMainPage.strIsCompleted = strIsCompleted;
oMainPage.strStatus = strStatus;
oMainPage.strIsDeleted = strIsDeleted;
oMainPage.strRemark = strRemark;
oMainPage.strRepeatId = strRepeatId;
oMainPage.strAlertCount = strAlertCount;
oMainPage.taskType = strTaskType;
oMainPage.totalTask = strTotalTask;
oMainPage.completedTask = strCompletedTask;
[tempArray addObject:oMainPage];
[oMainPage release];
}
}
sqlite3_finalize(statement);
} else {
NSAssert1(0, #"Failed to open database with message '%s'.", sqlite3_errmsg(appDelegate.database));
}
[oClsDBFunction release];
return tempArray;
}
Cheers.

Related

Passing variable in query does not show any result

I have table in sqlite I want to get data where userNAme and organizatiocode. problem is that it does not show any rows. If I do not pass variable and select all data then it returns rows.
Here is my code
+(void)getInitialData:(NSString *)dbPath {
MultipleDetailViewsWithNavigatorAppDelegate *appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSLog(#"User NAme is %#",appDelegate.userName);
// const char *sql = "select * from library";
const char *sql = "select * from library where userName=?";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
//sqlite3_bind_text(selectStmt, 1, [appDelegate.userName UTF8String], -1, SQLITE_TRANSIENT);
//sqlite3_bind_text(selectStmt, 1, [appDelegate.organizationCode UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(selectStmt , 1, [appDelegate.userName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(selectStmt , 2, [appDelegate.organizationCode UTF8String], -1, SQLITE_TRANSIENT);
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
coffeeObj.userID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
coffeeObj.contentAddedDateTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,2)];
coffeeObj.contentType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,3)];
coffeeObj.contentTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,4)];
coffeeObj.contentSource = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,5)];
coffeeObj.contentDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,6)];
coffeeObj.categoryTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,7)];
coffeeObj.subCategoryTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,8)];
coffeeObj.organizationCode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,9)];
coffeeObj.userName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,10)];
int count=[appDelegate.libraryArrayLocal count];
NSLog(#"count is beofore getting values %d",count);
[appDelegate.libraryArrayLocal addObject:coffeeObj];
int countone=[appDelegate.libraryArrayLocal count];
NSLog(#"count is after getting values %d",countone);
[coffeeObj release];
}
}
}
else
sqlite3_close(database);
}
}
Try to use:
NSString *query = [NSString stringWithFormat:#"select * from library where userName=%#",appDelegate.userName];
const char *sql = [query UTF8String];
Instead of:
const char *sql = "select * from library where userName=?";
also try without binding,
if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
//do your stuff here
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);

how to select unique values from sqlite table in iphone

I have data in my sqlite table like the following
here is the my table description and fields
coffeeID;
category;
sub_Category;
content_Type;
content_Title;
content_Description;
content_Id;
publisher;
content_Source;
update_date_time;
and here is the code where i am now getting all values but i want unique values.
const char *sql = "select * from category";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
coffeeObj.category=sqlite3_column_int(selectstmt,1);
coffeeObj.sub_Category = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
coffeeObj.content_Type = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
coffeeObj.content_Title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,4)];
coffeeObj.publisher = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];
coffeeObj.content_Description = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];
coffeeObj.content_Id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 7)];
coffeeObj.content_Source = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 8)];
int count=[appDelegate.coffeeArray count];
NSLog(#"count is beofore getting values %d",count);
[appDelegate.coffeeArray addObject:coffeeObj];
int countone=[appDelegate.coffeeArray count];
NSLog(#"count is after getting values %d",countone);
[coffeeObj release];
}
}
}
I want that if it has two same sub categories then it may show only one like mean only unique records not the categories with same name repeated.
You can use Group by in SQL Query
"Select * From category GROUP BY category"
You can group by any column

How to get int data from iphone sqlite data base

I am getting data from iphone sqlite like this. I have 6 records in the table.
My table has fields responses_Id, participant_Id, answer_option, answer_text, update_date_time only all are varchar fields except responses_Id and participant_Id)
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
CereniaAppDelegate *appDelegate = (CereniaAppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select * from survey_question_responses";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
coffeeObj.participant_Id=(This is integerin table)
coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];
NSString*test=coffeeObj.answer_option;
[appDelegate.coffeeArray addObject:coffeeObj];
int count=[appDelegate.coffeeArray count];
NSLog(test);
NSLog(#"count is %d",count);
[coffeeObj release];
}
}
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
When I NSlog data it does not show any things.
Look into the SQLite Documentation. All result values from queries are defined there.
You can use sqlite3_column_int for Integers similar to sqlite3_column_text for Text
NSNumber *primaryKey = [NSNumber numberWithInt:(int)sqlite3_column_int(selectstmt, 0)];
while(sqlite3_step(selectAllStmt) == SQLITE_ROW)
{
NSLog(#"record found");
char *temp = (char *)sqlite3_column_text(selectAllStmt, 0);
if (temp!=nil) {
intId=[[NSString stringWithUTF8String:temp] intValue];
}
temp =NULL;
// [pool release];
}
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
CereniaAppDelegate *appDelegate = (CereniaAppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select * from survey_question_responses";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey;
NSInteger intparticipant_Id;
char *temp = (char *)sqlite3_column_text(selectstmt, 0);
if (temp!=nil) {
primaryKey=[[NSString stringWithUTF8String:temp] intValue];
}
temp =NULL;
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
temp = (char *)sqlite3_column_text(selectstmt, 1);
if (temp!=nil) {
intparticipant_Id=[[NSString stringWithUTF8String:temp] intValue];
}
temp =NULL;
coffeeObj.participant_Id=intparticipant_Id;
coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];
NSString*test=coffeeObj.answer_option;
[appDelegate.coffeeArray addObject:coffeeObj];
int count=[appDelegate.coffeeArray count];
NSLog(test);
NSLog(#"count is %d",count);
[coffeeObj release];
}
}
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
Note:- char *temp = (char *)sqlite3_column_text(selectAllStmt, 0);
if(temp != NULL)
str = [NSString stringWithUTF8String:temp];
else
str = #"";
temp =NULL;
If you want to take integer then you can do type casting "str = [NSString stringWithUTF8String:temp];" here......

How to retrieve a particular column from a database in iphone

in my project im using a database.There are almost 365 questions in that.so i want to get a particulat question for a particular day.i used the code below to fetch a qusestion from database.
-(void)print{
sqlite3_stmt *statement;
// SELECT * from light where rowid = %i",1
qsql=[NSString stringWithFormat:#"Select * from ishh where col_1 = '365'"];
if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
NSLog(#"%dsssssssssssssss",sqlite3_step(statement));
NSLog(#"%ddddddddddddddddddddd", (SQLITE_ROW));
NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
NSLog(#"%# gg",Q_NO);
when i use the above code it is printing the correct question from database.Also when i give the statement like qsql=[NSString stringWithFormat:#"Select * from ishh where col_1 = '1'"]; it is not fetching the question.
But when i use the below code it is not fetching from the database.
-(void)print{
sqlite3_stmt *statement;
// SELECT * from light where rowid = %i",1
qsql=[NSString stringWithFormat:#"Select * from ishh where col_1 = '1'"];
if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
NSLog(#"%dsssssssssssssss",sqlite3_step(statement));
NSLog(#"%ddddddddddddddddddddd", (SQLITE_ROW));
NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
NSLog(#"%# gg",Q_NO);
while (sqlite3_step(statement) == (SQLITE_ROW))
{
NSLog(#" iolo");
NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
//NSString *Q_NO = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%i",sqlite3_column_int(statement, 0)]];
NSLog(#"%# gg",Q_NO);
}
sqlite3_reset(statement);
}
sqlite3_finalize(statement);
}
Can anyone tell me where im going wrong.Thanks in advance.
-(void)print{
sqlite3_stmt *statement;
qsql=[NSString stringWithFormat:#"Select * from ishh where col_1 = '1'"];
if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == (SQLITE_ROW))
{
char *field0 = (char *)sqlite3_column_text(statement, 0);
char *field1 = (char *)sqlite3_column_text(statement, 1);
NSString *Q_NO = nil;
if(field0!=NULL){
Q_NO = [[NSString alloc] initWithUTF8String:field0];
}
NSString *Q_NO1 = nil;
if(field1!=NULL){
Q_NO1 = [[NSString alloc] initWithUTF8String:field1];
}
NSLog(#"%# %#",Q_NO, Q_NO1);
[Q_NO release];
[Q_NO1 release];
}
}
sqlite3_finalize(statement);
}

iPhoneSdk, SQLite database search query not work

i want to know how to pass particular word or character in search query to find records..
i am passing in the format like '%%%#%%'.
but it doesn't give me output...
this is my code...
searchName = [NSString stringWithFormat:#"'%%%#%%'",[[NSUserDefaults standardUserDefaults] objectForKey:#"key"]];
-(void)getSearchData
{
NSLog(#"search Name %#", searchName);
searchArray = [[NSMutableArray alloc] init];
const char *sql = "select * from features where lower(name) like ? limit 500";
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL)!= SQLITE_OK)
{
NSAssert1(0,#"error : failed to prepare statement with message '%s'.",sql ite3_errmsg(database));
}
sqlite3_bind_text(selectstmt, 1, [searchName UTF8String], -1, SQLITE_TRANSIENT);
// sqlite3_bind_text(selectstmt, 2, [state UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSMutableDictionary *datah = [[NSMutableDictionary alloc] init];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,0)] forKey:#"number"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)] forKey:#"name"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,2)] forKey:#"type"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,4)] forKey:#"country"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,5)] forKey:#"lat"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,6)] forKey:#"long"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,7)] forKey:#"elevation"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,8)] forKey:#"area"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,3)] forKey:#"state"];
[searchArray addObject:datah];
[datah release];
}
sqlite3_reset(selectstmt);
}
if i pass a same query with word "s" i got 100 of record in sqlite query builder but if m pass through above code it doesn't serach any record
please help me
I worked on the code given above and fixed it as follows
-(void)getSearchData:(NSString*)searchText
{
**NSString* searchName = [NSString stringWithFormat:#"%%%#%%",searchText];**
NSLog(#"search Name %#", searchName);
sqlite3 *database;
sqlite3_stmt *selectstmt;
NSString* searchQuery = #"SELECT * FROM contacts WHERE lower(name) like ?";
const char *sql = [searchQuery UTF8String];
**if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {**
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) != SQLITE_OK)
{
//NSAssert1(0,#"error : failed to prepare statement with message '%s'.",sql ite3_errmsg(database));
}
sqlite3_bind_text(selectstmt, 1, [searchName UTF8String], -1, SQLITE_STATIC);
while (sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSLog(#"%#",[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,0)]);
}
sqlite3_reset(selectstmt);
}
sqlite3_close(database);
}
The changed portions can be found between Double *. you would also like to read this
Two steps that I changed were opening the database connection which You must have done elsewhere and second removing the '' from searchName. Do let us know if this resolves the issue.