3

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

}

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

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

  • 你检查 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");
    }
}
于 2013-01-01T18:40:24.343 回答
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 文件添加到正确的目标成员中。

于 2015-04-22T19:20:26.657 回答