0

うまくいけば、これは理にかなっています。ドキュメントフォルダーに sqlite データベースがあります。そのファイルがディレクトリに存在するかどうかを確認する機能もあり、存在しない場合はメインバンドルからそこに移動されます。したがって、データベースがアクセスするのに適切な場所にあることがわかります。ただし、アプリを実行すると、次のエラーが表示されます: no such table: 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

代わりにこの方法を使用してください。中間ディレクトリの手順をスキップして、データベースに直接話しかけます。

- (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 に答える
0

表示されるエラーは、テーブル 'databaseName' がデータベース ファイルに存在しないことを示しています。

データベースを開くことができないというエラーは発生していないので、それは問題ではありません。

sqlStatement が間違っているようです。

于 2012-06-21T18:51:31.823 に答える