开发者

Search in sqlite3 Database in iPhone

I want search the employeeName in data base my query is OK but it give NScFstring error when it 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 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"];
                 }
开发者_如何学JAVA         }
}

--> This line it 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 to open database with 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 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"];


@sam It seems as if there are no records in the database. If you are trying to set a nil object in dictionary it will give a crash for sure.Try to put a nil check before you add anything in dictionary.

You can also try to get the count of records matched and only when you get some result then try to set your data in dictionary ..

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜