当这个程序运行这段代码连接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
谢谢!