为什么一个陈述有效,一个陈述无效

时间:2014-09-19 18:59:35

标签: ios sql objective-c database sqlite

我在数据库中有一个表,我可以这样访问:

+(NSMutableArray *) queryDatabaseWithSQL:(NSString *)sql params:(NSArray *)params {
    sqlite3 *database;
    NSMutableArray *rtn = [[NSMutableArray alloc] init];
    int index = 0;
    NSString *filepath = [self copyDatabaseToDocumentsWithName:@"database"];
    if (sqlite3_open([filepath UTF8String], &database) == SQLITE_OK) {
        const char *sqlStatement = [sql cStringUsingEncoding:NSASCIIStringEncoding];
        sqlite3_stmt *compiledStatement;
        if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            if ([params count]) {
                for (int i = 0; i < [params count]; i++) {
                    NSString *obj = [params objectAtIndex:i];
                    sqlite3_bind_blob(compiledStatement, i + 1, [obj UTF8String], -1, SQLITE_TRANSIENT);
                }
            }
            while (sqlite3_step(compiledStatement) == SQLITE_ROW) {
                NSMutableArray *arr = [[NSMutableArray alloc] init];
                index = 0;
                const char *s = (char *)sqlite3_column_text(compiledStatement, index);
                while (s) {
                    [arr addObject:[NSString stringWithUTF8String:s]];
                    index++;
                    s = (char *)sqlite3_column_text(compiledStatement, index);
                }
                if (![rtn containsObject:arr]) {
                    [rtn addObject:arr];
                }
            }
        }
        sqlite3_finalize(compiledStatement);
    }
    NSLog(@"ERROR: %s", sqlite3_errmsg(database));
    sqlite3_close(database);
    return rtn;
}

当我调用这样的函数时,这很好用:

NSLog(@"%@", [database queryDatabaseWithSQL:@"SELECT * FROM FRIENDSUSERS WHERE USER = ?" params:@[[delegate->user objectForKey:@"username"]]]);

然后当我使用这样的字符串调用函数时,它不会返回任何行:

NSLog(@"%@", [database queryDatabaseWithSQL:@"SELECT * FROM FRIENDSUSERS WHERE USER = ?" params:@[@"username"]]);

我还没有弄清楚会发生什么,但是我已经检查了字符串匹配,他们这样做我现在已经卡住了

任何人都可以看到为什么这不起作用的原因

我也运行了错误检查,每次都没有返回错误或行:(

提前致谢

0 个答案:

没有答案