Search in sqlite3 Database in iPhone -
i want search employeename in data base query ok give nscfstring error when bind. please help.
code is:
sqlite3 *database; self.array_employeesearch = nil; [self.array_employeesearch release]; self.array_employeesearch = [[nsmutablearray alloc]init]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentdirectory = [paths objectatindex:0]; nsstring *path = [documentdirectory stringbyappendingpathcomponent:@"employee.sqlite"]; if(sqlite3_open([path utf8string], &database) == sqlite_ok){ nsstring *str_query = [nsstring stringwithformat:@"select empname employee empname '%@%@%@'",@"%",str_emp,@"%"]; const char *sql = [str_query utf8string]; sqlite3_stmt *statement; if(sqlite3_prepare_v2(database, sql, -1, &statement, null) == sqlite_ok){ while (sqlite3_step(statement) == sqlite_row) { nsmutabledictionary *dict_employee; dict_employee = [[nsmutabledictionary alloc]init]; [dict_employee setvalue:[nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 2)] forkey:@"id"]; } } }
--> line crash....
[dict_employee setvalue:[nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 3)] forkey:@"empserverid"]; [dict_employee setvalue:[nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 4)] forkey:@"name"]; [dict_employee setvalue:[nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 5)] forkey:@"username"]; [dict_employee setvalue:[nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 6)] forkey:@"password"]; [dict_employee setvalue:[nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 7)] forkey:@"email"]; [dict_employee setvalue:[nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 8)] forkey:@"phone"]; [dict_employee setvalue:[nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 9)] forkey:@"status"]; nsstring *isdelete; isdelete = [nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 10)]; if ([isdelete isequaltostring:@"false"]) { [array_employeesearch addobject:dict_employee]; } dict_employee = nil; [dict_employee release]; } } sqlite3_finalize(statement); } else { sqlite3_close(database); nsassert1(0, @"failed open database message '%s'.", sqlite3_errmsg(database)); }
try this
sqlite3 *database; self.array_employeesearch = nil; [self.array_employeesearch release]; self.array_employeesearch = [[nsmutablearray alloc]init]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentdirectory = [paths objectatindex:0]; nsstring *path = [documentdirectory stringbyappendingpathcomponent:@"employee.sqlite"]; if(sqlite3_open([path utf8string], &database) == sqlite_ok){ const char *sql = "select empname employee empname '%?%'"; sqlite3_stmt *statement; if(sqlite3_prepare_v2(database, sql, -1, &statement, null) == sqlite_ok){ // query, bind primary key first (and only) placeholder in statement. // note parameters numbered 1, not 0. sqlite3_bind_int(init_statement, 1, str_emp); while (sqlite3_step(statement) == sqlite_row) { nsmutabledictionary *dict_employee; dict_employee = [[nsmutabledictionary alloc]init]; [dict_employee setvalue:[nsstring stringwithutf8string:(char *)sqlite3_column_text(statement, 2)] forkey:@"id"];
Comments
Post a Comment