Поиск в базе данных sqlite3 в iPhone

#iphone

#iPhone

Вопрос:

Я хочу выполнить поиск по имени сотрудника в базе данных, мой запрос в порядке, но при привязке выдает ошибку 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], amp;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, amp;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));
    }
  

Ответ №1:

Попробуйте это

         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], amp;database) == SQLITE_OK){

            const char *sql = "select EmpName from Employee where EmpName like '%?%'";

            sqlite3_stmt *statement;

            if(sqlite3_prepare_v2(database, sql, -1, amp;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"];
  

Ответ №2:

@sam Кажется, что в базе данных нет записей. Если вы пытаетесь установить нулевой объект в словаре, это наверняка приведет к сбою.Попробуйте установить нулевую проверку, прежде чем добавлять что-либо в dictionary.

Вы также можете попытаться сопоставить количество записей, и только когда вы получите какой-либо результат, попробуйте установить свои данные в словаре..

Приветствия