简体   繁体   English

iPhone - 尝试将sqlite数据库复制到文档目录 - 复制空白版本

[英]iPhone - Trying to Copy sqlite Database to Documents Directory - copies blank version

I have an sqlite database called 'ProductDatabase.sql', which I have copied into my applications project directory: 我有一个名为'ProductDatabase.sql'的sqlite数据库,我已将其复制到我的应用程序项目目录中:

/Users/jacknutkins/Documents/TabbedDietApp/TabbedDietApp/ProductDatabase.sql /Users/jacknutkins/Documents/TabbedDietApp/TabbedDietApp/ProductDatabase.sql

In the applications app delegate class I have this piece of code: 在应用程序app委托类中,我有这段代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Set-up some globals
    m_DatabaseName = @"ProductDatabase.sql";

    //Get the path to the documents directory and append the databaseName
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    m_DatabasePath = [documentsDirectory stringByAppendingPathComponent:@"ProductDatabase.sql"];

    //Execute the "checkAndCreateDatabase" function
    [self checkAndCreateDatabase];

    //Query the databse for all animal records and construct the "animals" array
    [self readProductsFromDatabase];

....

At this point: 这一点:

m_DatabasePath = '/Users/jacknutkins/Library/Application Support/iPhone Simulator/5.0/Applications/6D5BBE3A-BC9A-4C44-B089-FABA27CFFF4B/Library/ProductDatabase.sql' m_DatabasePath ='/ Users / jacknutkins / Library / Application Support / iPhone Simulator / 5.0 / Applications / 6D5BBE3A-BC9A-4C44-B089-FABA27CFFF4B / Library / ProductDatabase.sql'

Here is the code for the other 2 methods: 以下是其他两种方法的代码:

- (void) checkAndCreateDatabase {
    NSError * error;

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

    //Create a file manager object, we will use this to check the status
    //of the databse and to copy it over if required
    NSFileManager *l_FileManager = [NSFileManager defaultManager];

    //Check if the database has already been created in the users filesystem
    l_Success = [l_FileManager fileExistsAtPath:m_DatabasePath];

    //If the database already exists then return without doing anything

    if(l_Success)
        return;

    //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 *l_DatabasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:m_DatabaseName];

    //Copy the database from the package to the usrrs filesystem
    [l_FileManager copyItemAtPath:l_DatabasePathFromApp toPath:m_DatabasePath error:&error];

}

If I perform some NSLogs here: 如果我在这里执行一些NSLog:

l_DatabasePathFromApp = /Users/jacknutkins/Library/Application Support/iPhone Simulator/5.0/Applications/6D5BBE3A-BC9A-4C44-B089-FABA27CFFF4B/TabbedDietApp.app/ProductDatabase.sql l_DatabasePathFromApp = / Users / jacknutkins / Library / Application Support / iPhone Simulator / 5.0 / Applications / 6D5BBE3A-BC9A-4C44-B089-FABA27CFFF4B / TabbedDietApp.app / ProductDatabase.sql

and: 和:

error = Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn't be completed. (Cocoa error 260.)" UserInfo=0x6b6d060 {NSFilePath=/Users/jacknutkins/Library/Application Support/iPhone Simulator/5.0/Applications/6D5BBE3A-BC9A-4C44-B089-FABA27CFFF4B/TabbedDietApp.app/ProductDatabase.sql, NSUnderlyingError=0x6b6cfa0 "The operation couldn't be completed. No such file or directory"} error = Error Domain = NSCocoaErrorDomain Code = 260“操作无法完成。(Cocoa错误260.)”UserInfo = 0x6b6d060 {NSFilePath = / Users / jacknutkins / Library / Application Support / iPhone Simulator / 5.0 / Applications / 6D5BBE3A- BC9A-4C44-B089-FABA27CFFF4B / TabbedDietApp.app / ProductDatabase.sql,NSUnderlyingError = 0x6b6cfa0“操作无法完成。没有这样的文件或目录”}

I'm not sure what file it is it can't find here.. 我不确定它在这里找不到什么文件..

- (void) readProductsFromDatabase {
    //Init the products array
    m_Products = [[NSMutableArray alloc] init];
    NSLog(@"%@", m_DatabasePath);
    //Open the database from the users filessystem
    if(sqlite3_open([m_DatabasePath UTF8String], &database) == SQLITE_OK) {
        //Set-up the SQL statement and compile it for faster access
        const char *sqlStatement = "select * from products";
        sqlite3_stmt *compiledStatement;

        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
        {
            NSLog(@"Success..");
            //Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                //Read the data from the results row
                NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSString *aCategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                NSString *aCalories = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
                NSString *aFat = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
                NSString *aSaturates = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
                NSString *aSugar = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];
                NSString *aFibre = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)];
                NSString *aSalt = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)];
                NSString *aImageURL = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)];
                NSLog(@"Delegate");
                NSString *aNote = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 10)];
                NSUInteger myInt = sqlite3_column_int(compiledStatement, 11);
                NSString *aServes = [NSString stringWithFormat:@"%d", myInt];

                //Create a new animal object with the data from the database
                Product *l_Product = [[Product alloc] initWithName:aName category:aCategory calories:aCalories fat:aFat saturates:aSaturates sugar:aSugar fibre:aFibre salt:aSalt imageURL:aImageURL note:aNote serves:aServes];

                //Add the animal object to the animals array
                [m_Products addObject:l_Product];

            }
        }
        //Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);

}

database is declared in the .h file as follows: 数据库在.h文件中声明如下:

//Setup the database object
    sqlite3 *database;

In the above method, the line: 在上面的方法中,行:

if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)

does not evaluate to SQLITE_OK, because the database I try to copy to the documents directory is blank. 不评估为SQLITE_OK,因为我尝试复制到文档目录的数据库是空白的。

I have tried cleaning and building, deleting the blank copy of the database and re-running etc but it continues to copy a blank database every time. 我尝试过清理和构建,删除数据库的空白副本并重新运行等,但每次都会继续复制空白数据库。

I've googled this several times and I've tried everything I can find with no success.. 我已经用Google搜索了几次,我已经尝试了所有我找不到的东西但没有成功。

Any help would be hugely appreciated. 任何帮助将非常感激。

Jack 插口

EDIT 编辑

If I perform 'select * from products' from the terminal window on the database in the project directory I return the expected results. 如果我从项目目录中数据库的终端窗口执行'select * from products',我会返回预期的结果。

我忘记了通过单击文件检查器选项卡中的复选框将数据库文件包含在目标成员资格中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将项目从Documents目录复制到iPhone App中的.app目录? - How to copy items from Documents directory to .app directory in iPhone App? 试图在iPhone应用程序更新中覆盖sqlite数据库 - Trying to overwrite sqlite database in iPhone app update iPhone的任何接口来处理在应用程序的Documents文件夹中创建的sqlite数据库? - iphone any interface to deal with sqlite database created in the application Documents folder? iPhone:Documents目录的可写性 - iPhone: Writability of the Documents directory iCloud是否会自动备份document目录中的Core Data sqlite数据库? - Is Core Data sqlite database in the documents directory automatically backed up by iCloud? 我应该将sqlite数据库文件写入Documents目录还是Library / Caches? - Should i write sqlite database file to Documents directory or Library/Caches? 目标c-iPhone-替换sqlite数据库的本地副本 - objective c - iPhone - replacing local copy of sqlite database 当iPhone中的Appstore上的应用程序版本更改时,sqlite数据库更新 - sqlite database update when app version changes on Appstore in iPhone XCode在进行构建时会复制空白数据库 - XCode copies blank database when doing build 如何在应用程序版本更新的情况下在iPhone上的SQLite数据库中维护数据? - How to maintain data in an SQLite Database on an iPhone at a version update of the application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM