if(sqlite3_step(compiledStatement)== SQLITE_ROW)失败

时间:2014-03-11 20:49:37

标签: ios sqlite

在第一个实现文件中,所有步骤

- (void) readCitiesFromDatabase {

    databaseName = @"123.sqlite";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    // Setup the database object
    sqlite3 *database;

    // Setup the SQL Statement and compile it for faster access
    NSString *querySQL = [NSString stringWithFormat:@"SELECT City_Name, City_Picture, City_Id FROM CITIES, COUNTRIES WHERE CITIES.Country_Id=COUNTRIES.Country_Id AND COUNTRIES.Country_Id = '%@'", self.countryID];

    const char *sqlStatement = [querySQL UTF8String];
    sqlite3_stmt *compiledStatement;

    // Init the countries Array
    self.cities = [[NSMutableArray alloc] init];

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        NSLog(@"success");

        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            NSLog(@"success");
            while(sqlite3_step(compiledStatement) == SQLITE_ROW)
            {
                NSLog(@"success");
                // Read the data from the result row
                char *temp_cName = (char *)sqlite3_column_text(compiledStatement, 0);
                NSString *cName = temp_cName == NULL ? nil : [[NSString alloc] initWithUTF8String:temp_cName];

                char *temp_cPicture = (char *)sqlite3_column_text(compiledStatement, 1);
                NSString *cPicture = temp_cPicture == NULL ? nil : [[NSString alloc] initWithUTF8String:temp_cPicture];

                char *temp_cId = (char *)sqlite3_column_text(compiledStatement, 2);
                NSString *cId = temp_cId == NULL ? nil : [[NSString alloc] initWithUTF8String:temp_cPicture];

                NSMutableDictionary *city = [[NSMutableDictionary alloc] init];
                [city setObject:cName forKey:@"City_Name"];
                [city setObject:cPicture forKey:@"City_Picture"];
                [city setObject:cId forKey:@"City_Id"];

                [self.cities addObject:city];
            }
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);   
    }
    sqlite3_close(database);
}

现在我得到了城市名单。当我选择一个城市时,我看不到城市信息。 这是第二次实施。

- (void) readCityInfoFromDatabase:(NSString *)querySQL
{
    databaseName = @"123.sqlite";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    // Setup the database object
    sqlite3 *database;

    // Setup the SQL Statement and compile it for faster access

    const char *sqlStatement = [querySQL UTF8String];
    sqlite3_stmt *compiledStatement;

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        NSLog(@"success");
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            NSLog(@"success");
            if (sqlite3_step(compiledStatement) == SQLITE_ROW)
            {
                NSLog(@"success");

                char *temp_content = (char *)sqlite3_column_text(compiledStatement, 0);
                [self.cityTextView setText:temp_content == NULL ? nil : [[NSString alloc] initWithUTF8String:temp_content]];
            }
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}

前两步是成功,但是,

    if (sqlite3_step(compiledStatement) == SQLITE_ROW)
    {
        NSLog(@"success");

我在这一步上看不到成功。

请帮忙。

1 个答案:

答案 0 :(得分:1)

你真的应该保存sqlite3_step()的结果并检查它是否是:

  • SQLITE_ROW =检索到的数据行
  • SQLITE_DONE =没有(更多)要检索的数据
  • 其他=发生了一些错误

根据您分享的内容,您很有可能只是收到SQLITE_DONE,因为找不到任何内容。如果SQL中存在错误(例如错误连接,错误的WHERE子句等),您可以得到它。如果没有看到readCityInfoFromDatabase使用的SQL和数据库中的一些代表性数据,很难说。

顺便说一句,不仅是sqlite3_step(),而是所有sqlite3_xxx()次来电,如果收到错误,您应该记录sqlite3_errmsg()。现在,如果出现错误,您必须猜测问题的根源是什么。