我想在数据库中搜索employeeName,我的查询没问题,但在绑定时会出现NScFstring错误。 请帮忙。
代码是:
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 :(得分: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"];
答案 1 :(得分:0)
@sam好像数据库中没有记录。如果你试图在字典中设置一个nil对象,肯定会发生崩溃。试着在你在字典中添加任何内容之前进行一次nil检查。
你也可以尝试匹配记录的计数,只有当你得到一些结果然后尝试在字典中设置你的数据..
干杯