【问题标题】:Can't copy sqlite database from app's mainbundle to users documents无法将 sqlite 数据库从应用程序的 mainbundle 复制到用户文档
【发布时间】:2012-12-16 06:53:04
【问题描述】:

我无法将我的 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");
    }
}

}

【问题讨论】:

  • 这个问题的题目是错误的。你想从 INSIDE 包复制到文件夹,而不是从项目目录 - 编辑它

标签: iphone ios database sqlite ios-simulator


【解决方案1】:

有一个逻辑错误,你检查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)
{
   ...
}

【讨论】:

  • @user1463853:没问题,很乐意提供帮助。
【解决方案2】:

你打开一个数据库(如果它不存在,则使用sqlite3_open 创建一个新的 0 字节,然后将发送的一个复制到新数据库应该在的位置。

  • 您检查 DB_docs 是否存在
  • 但是无论如何你都用 sqlite 打开 DB_docs
  • 然后您尝试将其从捆绑包复制到打开的文件路径中

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

【讨论】:

    【解决方案3】:

    我只是用AppDelegate写的如下方法, (貌似方法体很大,其实不然。把所有的cmets都去掉,看完cmets再看清楚):

    #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 文件添加到正确的目标成员中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-27
      • 2011-03-20
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多