0

希望这是有道理的。我的文档文件夹中有一个 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);
}
4

2 回答 2

0

您收到的错误表明您的数据库文件中不存在表“databaseName”。

您没有收到无法打开数据库的错误,所以这不是问题。

看起来你的 sqlStatement 是错误的。

于 2012-06-21T18:51:31.823 回答
0

改用这种方式。跳过中间目录步骤,直接与数据库对话。

- (void)checkAndCreateDB {

    NSString *dbPath = [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"../Documents/yourDB.sqlite"];

    // Check if the SQL database has already been saved to the users phone, if not then copy it over
    BOOL success;

    // Check if the database has already been created in the users filesystem
    success = [[NSFileManager defaultManager] fileExistsAtPath:dbPath];

    if(!success) {

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"yourDB.sqlite"];

    [fileManager copyItemAtPath:databasePathFromApp toPath:dbPath error:nil];

    NSLog(@"readandwrite is available");
    }

if(!(sqlite3_open([dbPath UTF8String], &dB) == SQLITE_OK))
    {
        NSLog(@"An error has occurred.");
    }

}
于 2012-06-21T18:36:11.190 回答