2011-05-11 43 views
4

我想在數據庫中搜索employeeName我的查詢是可以的,但是當它綁定時它會給出NScFstring錯誤。 請幫忙。在iPhone中的sqlite3數據庫中搜索

代碼:

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 from Employee where EmpName like '%@%@%@'",@"%",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"]; 
       } 
     } 
} 

- >這一行就崩潰....

[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 to open database with message '%s'.", sqlite3_errmsg(database)); 
    } 

回答

0

試試這個

 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 from Employee where EmpName like '%?%'"; 

      sqlite3_stmt *statement; 

      if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK){ 
      // For this query, we bind the primary key to the first (and only) placeholder in the statement. 
      // Note that the parameters are numbered from 1, not from 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"]; 
0

@sam看起來好像沒有記錄在數據庫中。如果你試圖在字典中設置一個零對象,它肯定會崩潰。嘗試在字典中添加任何內容之前先進行無檢查。

您也可以嘗試得到計數的記錄匹配,只有當你得到那麼一些結果嘗試在字典中設置您的數據..

乾杯