Я ориентируюсь на iOS 5.1 и пытаюсь скопировать базу данных из файлов приложения в папку «Мои документы».Я делал это с тем же кодом в приложениях в прошлом, поэтому я немного запутался, почему он не работает на этот раз.
Это метод, который я использую, чтобы проверить, если онсуществует и скопируйте его, если нет.Это из здесь .
-(void) checkAndCreateDatabase{
// 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.databasePath];
// If the database already exists then return without doing anything
if(success) return;
NSLog(@"Installing db: %@", self.databasePath);
// 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:databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:self.databasePath error:nil];
//[databasePathFromApp release];
//[fileManager release];
}
Когда я пытаюсь сделать запрос к БД, я делаю так:
sqlite3 *database1;
// Open the database from the users filessytem
if(sqlite3_open([self.databasePath UTF8String], &database1) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
NSString *sqlStatement = [NSString stringWithFormat: @"update login set is_logged=0"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database1, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) {
if(SQLITE_DONE != sqlite3_step(compiledStatement)){
NSAssert1(0, @"Error while updating logged. '%s'", sqlite3_errmsg(database1));
NSLog(@"Error setting logged_in to 0: %s", sqlite3_errmsg(database1));
}
else{
NSLog(@"Made Logged_in=0");
}
}
else{
NSLog(@"Prop problem1: %s", sqlite3_errmsg(database1));
NSLog(@"Couldn't prep 1");
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
else{
NSLog(@"Couldn't even open db1");
}
sqlite3_close(database1);
The *Функция 1013 * возвращает false в этом случае с sqlite3_errmsg
из no such table: login
.
У меня есть функция, которая вызывается каждую секунду и создает объект, который использует этот объект базы данных.Может ли быть так, что база данных не была скопирована в эту секунду, и следующий вызов прерывает предыдущую копию?Это не кажется вероятным.
Есть идеи, в чем может быть проблема?
Решение Я задавался этим вопросом: здесь и согласно пунктуномер 2 кажется, что моей базы данных не было в списке «Copy Bundle Resources».Я добавил его, и теперь все в порядке.
Спасибо, что заставили меня задуматься в правильном направлении, ребята.