0

当这个程序运行这段代码连接SQLite数据库时,它可以通过路径测试,打开数据库,SQL在实际数据库上运行。然后它在尝试执行 sqlite3_prepare 语句时失败。它以代码 1 退出 - SQL 错误或缺少数据库。然后它将继续执行该程序,直到它尝试读取从数据库中获取的数据。然后它失败了,因为它没有得到任何东西,因为它返回了 SQL 错误或 sqlite3_prepare 语句中缺少数据库代码。我正在寻找错误的代码部分/我可以更正以使其在 sqlite3_prepared 语句上成功。

#import "contactDAO.h"
#import "contact.h"

@implementation contactDAO

- (NSMutableArray *) getList{
NSMutableArray *contactArray = [[NSMutableArray alloc] init];
@try {
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"iFLY.sqlite"];
    BOOL success = [fileMgr fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured.");
    }
    const char *sql = "SELECT reportNumber, dateEntered, country, state FROM CONTACT";
    sqlite3_stmt *sqlStatement;
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement");
    }

    //
    while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
        Contact *contactInfo;

        contactInfo.reportNumber = sqlite3_column_int(sqlStatement, 0);
        contactInfo.dateEntered = sqlite3_column_int(sqlStatement, 1);
        contactInfo.country = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
        contactInfo.state = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,3)];
        /*contactInfo.county = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,4)];
        contactInfo.town = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,5)];
        contactInfo.policeContact = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,6)];
        contactInfo.policeReport = sqlite3_column_int(sqlStatement, 7);
        contactInfo.autopsyNumber = sqlite3_column_int(sqlStatement, 8);
        contactInfo.lastSeen = sqlite3_column_int(sqlStatement, 9);
        contactInfo.victimName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,10)]; */
        [contactArray addObject:contactInfo];
    }
}
@catch (NSException *exception) {
    NSLog(@"An exception occured: %@", [exception reason]);
}
@finally {
    return contactArray;
} 
}

@end

谢谢!

4

2 回答 2

0

CONTACT 是视图还是表?从视图中选择时,我遇到了类似的问题,得到了相同的错误。当我将其更改为表格时,它起作用了。

于 2012-09-27T15:09:41.607 回答
0

您缺少“db”的声明,如下所示:

sqlite3 *db;

我还可以看到一个清晰度问题:

if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))

由于即使发现问题,您的代码也会继续评估内容,因为您不会在尝试捕获错误的那些部分之后返回。

您的执行顺序应如下所示:

if (sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) 
{        
    const char *sql = "SELECT * FROM table";
    sqlite3_stmt *statement;        
    if (sqlite3_prepare_v2(db, sql, -1, &statement, NULL) == SQLITE_OK) 
    {
        while (sqlite3_step(statement) == SQLITE_ROW) { /* do stuff here */ }
    }
}
于 2012-04-23T03:06:52.113 回答