无法将sqlite数据库从app的mainbundle复制到用户文档

时间:2013-01-01 18:22:26

标签: iphone ios database sqlite ios-simulator

我无法将我的sqlite数据库复制到用户文档文件夹,数据库总是0字节但应该有322 kb。

我还检查过数据库是否包含在目标成员资格中。

问题是我无法正确复制数据库。

这是我的代码:

-(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");
    }
}

}

3 个答案:

答案 0 :(得分: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)
{
   ...
}

答案 1 :(得分:1)

你打开一个数据库(如果它不在那里使用sqlite3_open那么新的一个0字节,然后将发货的数据复制到新数据库所在的位置..

  • 检查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");
    }
}

答案 2 :(得分:0)

我只是使用AppDelegate编写的以下方法, (看起来方法体是巨大的,但事实上并非如此。只需删除所有评论,并在阅读评论后看清楚):

#pragma mark - SQLite DB
// SQLite Copy the Database.
-( void ) checkAndCreateDatabase
{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;

NSString *databasePath = [LIBRARY_DIR_PATH stringByAppendingPathComponent:DATABASE_NAME];
NSLog(@"%@",databasePath);

// 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:databasePath];
// If the database already exists then return without doing anything
if(success) return;

NSLog(@"DB WASN'T THERE! SO GONNA COPY IT!");

// 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:DATABASE_NAME];
success = [fileManager fileExistsAtPath:databasePathFromApp];
if (success) {
    NSLog(@"SIMULATOR FOUND DB FROM APP, GONNA COPY IT");
} else {
    NSLog(@"SIMULATOR COULDN'T FIND DB\nIT SHOULD BE IN : %@\nPlease check if you have added the DB file and selected its target", databasePathFromApp);
}

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

}

中调用该方法
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

如下:

//Copy the SQLite3 DB File
[self checkAndCreateDatabase];

提示:请记住,在将db文件拖放到XCode项目时,需要将SQLite DB文件添加到正确的目标成员资格中。