Невозможно заставить SQLite для iPhone использовать файл базы данных, который я предоставляю, и вместо этого он создает новый под Macintosh HD - PullRequest
1 голос
/ 09 сентября 2010

Я создаю проект базы данных SQLite3 в Xcode для Iphone.Я использую симулятор 3.1.2.Хотя я вложил файл базы данных в группу «Ресурсы» проекта и передал имя в файл Appdelegate, а также создал локальную редактируемую копию, используя:

// Copy the database from the package to the user's filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:self.dbPath error:nil];

... но каждый раз, когда я запускаюНапример, чтобы проверить, он создает новый файл базы данных в файловой системе с именем, которое я предоставляю в папке Macintosh HD.Я предоставляю имя базы данных следующим образом: @ "testdb.sqlite", и моя подпрограмма инициализации базы данных выглядит следующим образом:

- (id) initsql: (NSString*) name {
    if(self = [super init])
    {
        // Override point for customization after app launch    
        self.logging = -1;
        self.dbName = name;

        // Get the path to the documents directory and append the database name
        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [documentPaths objectAtIndex:0];
        self.dbPath = [documentsDir stringByAppendingPathComponent:self.dbName];

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

        // Create a FileManager object, we will use this to check the status
        // of the database and to copy it over if required
        NSFileManager *fileManager = [NSFileManager defaultManager];

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

        // If the database already exists then return without doing anything
        if(!success)
        {
            // If not then proceed to copy the database from the application to the users filesystem

            // Get the path to the database in the application package
            NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.dbName];

            // Copy the database from the package to the users filesystem
            [fileManager copyItemAtPath:databasePathFromApp toPath:self.dbPath error:nil];

            [fileManager release];
        }
        int result = sqlite3_open([self.dbName UTF8String], &database);
        if(result != SQLITE_OK)
        {
            sqlite3_close(database);
            [self logmsg:[[NSString alloc] initWithFormat:@"Couldn't open database: %@", self.dbName] level:2];
            return self;
        }
        [self logmsg:[[NSString alloc] initWithFormat:@"Database opened: %@", self.dbName] level:-1];
    }
    return self;
}

Есть ли какие-либо советы относительно того, что может быть не так?

1 Ответ

1 голос
/ 09 сентября 2010

решаемая.

int result = sqlite3_open([self.dbName UTF8String], &database);

... должно было быть ...

int result = sqlite3_open([self.dbPath UTF8String], &database);
...