1

大家好,我的查询一直得到结果 1,即使在 SQLite 查询浏览器上使用相同的语法

NSString *path = [[NSBundle mainBundle] pathForResource:@"amstations" ofType:@"db"];
sqlite3 *database;
if(sqlite3_open([path UTF8String],&database) == !SQLITE_OK) {
    NSLog(@"DB Did not open");
}
else {
    NSString *theState = [states objectAtIndex:[thePickerView selectedRowInComponent:1]];

    NSString *sqlStatement;
    if ([pickView selectedRowInComponent:0] == 0)
    {
        sqlStatement = [NSString stringWithFormat:@"SELECT * FROM stations WHERE state like '%@ %'",theState];
    }
    else if ([pickView selectedRowInComponent:0] == 1)
    {
        sqlStatement = [NSString stringWithFormat:@"SELECT * FROM stations WHERE state like '%@ %' ORDER BY format",theState];
    }
    else if ([pickView selectedRowInComponent:0] == 2)
    {
        sqlStatement = (@"SELECT state, power || ' kW' power FROM ( SELECT state, Cast(Rtrim(power, 'kW') AS 'numeric') power FROM stations WHERE state LIKE '%CA%' ORDER BY power)");
    }
    else
    {
        sqlStatement = [NSString stringWithFormat:@"SELECT * FROM stations WHERE state like '%@ %' ORDER BY city",theState];
    }

    sqlite3_stmt *statement;
    int result = sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &statement, nil);
    NSLog(@"result: %i", result);

    if (sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &statement, nil) == SQLITE_OK) {
        NSLog(@"prepare succeeded");
        NSLog(@"%@",sqlStatement);
        city = [[NSMutableArray alloc] init];

如果我输入一个简单的查询,例如“SELECT * FROM station WHERE state LIKE 'CA'”,它就可以工作。

4

0 回答 0