希望这是有道理的。我的文档文件夹中有一个 sqlite 数据库。我还有一个函数可以检查目录中是否存在该文件,如果不存在,则将其从主包中移到那里。所以我知道数据库位于正确的访问位置。但是,当我运行我的应用程序时,我收到错误:没有这样的表:databaseName。问题可能与sqlite数据库的读写访问有关吗?如果是这样,我如何检查并纠正问题?
#define kFilename @"foods.sqlite"
- (NSString *)dataFilePath {
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* sqliteFile = [documentsPath stringByAppendingPathComponent:kFilename];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:sqliteFile];
if(fileExists)
return sqliteFile;
else {
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kFilename];
NSString *folderPath = [documentsPath stringByAppendingPathComponent:kFilename];
NSError *error;
[[NSFileManager defaultManager] copyItemAtPath:sourcePath
toPath:folderPath
error:&error];
NSLog(@"Error description-%@ \n", [error localizedDescription]);
NSLog(@"Error reason-%@", [error localizedFailureReason]);
}
return [documentsPath stringByAppendingPathComponent:kFilename];
}
-(void) viewWillAppear:(BOOL)animated{
sqlite3 *database;
if (sqlite3_open([[self dataFilePath] UTF8String], &database)
!= SQLITE_OK) {
sqlite3_close(database);
NSAssert(0, @"Failed to open database");
}
sqlite3_stmt *statement;
//why is this if statement failing?
if (sqlite3_prepare_v2(database, [sqlStatement UTF8String],
-1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
//int row = sqlite3_column_int(statement, 0);
char *rowData = (char *)sqlite3_column_text(statement, 1);
foodName = [[NSString alloc] initWithUTF8String:rowData];
[foodArray addObject:foodName];
}
sqlite3_finalize(statement);
}
else {
NSLog(@"%i", sqlite3_prepare_v2(database, [sqlStatement UTF8String],
-1, &statement, nil));
NSLog(@"Statement: %s", sqlite3_errmsg(database));
}
sqlite3_close(database);
}