3

sqliteデータベースをusersdocumentsフォルダーにコピーできません。データベースには常に0バイトがありますが、322kbである必要があります。

また、データベースがターゲットメンバーシップに含まれていることを確認しました。

問題は、データベースを正しくコピーできないことです。

これが私のコードです:

-(void)initDatabase
{
// Create a string containing the full path to the sqlite.db inside the documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"wine.sqlite"];

// Check to see if the database file already exists
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];

// Open the database and store the handle as a data member
if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK)
{

    // Create the database if it doesn't yet exists in the file system
    if (!databaseAlreadyExists)
    {
        // Get the path to the database in the application package
        NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource:@"wine" ofType:@"sqlite"];
        //NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"wine.sqlite"];

        // Copy the database from the package to the users filesystem
        [[NSFileManager defaultManager] copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

        NSLog(@"Database created");
    }
}

}

4

3 に答える 3

4

論理エラーがありますdatabaseAlreadyExists

if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK) { ... }

ブロック。

あなたがおそらく意味したのは

// Check to see if the database file already exists
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];

// Create the database if it doesn't yet exists in the file system
if (!databaseAlreadyExists)
{
    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource:@"wine" ofType:@"sqlite"];

    // Copy the database from the package to the users filesystem
    [[NSFileManager defaultManager] copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

    NSLog(@"Database created");
}

// Open the database and store the handle as a data member
if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK)
{
   ...
}
于 2013-01-01T18:38:38.223 に答える
1

DB を開きます (存在しない場合は新しい 0 バイトをsqlite3_open作成し、出荷されたものを新しい DB があるべき場所にコピーします。

  • DB_docs があるかどうかを確認します
  • しかし、その後、何があってもsqliteでDB_docsを開きます
  • 次に、バンドルからOPENEDファイルパスにコピーしようとします

-(void)initDatabase
{
    // Create a string containing the full path to the sqlite.db inside the documents folder
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"wine.sqlite"];

    // Check to see if the database file already exists
    BOOL databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];

    // Create the database if it doesn't yet exists in the file system
    if (!databaseAlreadyExists)
    {
        // Get the path to the database in the application package
        NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource:@"wine" ofType:@"sqlite"];

        // Copy the database from the package to the users filesystem
        [[NSFileManager defaultManager] copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

        NSLog(@"Database created");
    }

    // Open the database and store the handle as a data member
    if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK)
    {
        NSLog(@"db opened");
    }
}
于 2013-01-01T18:40:24.343 に答える