iPhone - 更新表格后sqlite无法工作

4

我有以下两种方法从我的数据库获取数据:

- (void) checkAndCreateDatabase {
    //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 usrs 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:nil];

}

并且:

- (void) readProductsFromDatabase {
    //Setup the database object
    sqlite3 *l_Database;

    //Init the products array
    m_Products = [[NSMutableArray alloc] init];

    //Open the database from the users filessystem
    if(sqlite3_open([m_DatabasePath UTF8String], &l_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(l_Database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
        {
            //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(l_Database);
}

My problem appears to be that the line:

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

无法评估为SQLITE_OK。

这是我的数据库路径:

m_DatabasePath = /Users/jacknutkins/Library/Application Support/iPhone Simulator/5.0/Applications/6F723583-9A25-4F17-AF75-911DE9CB7BD8/Library/ProductDatabase.sql

这是我的应用程序委托方法,包含相关代码:

- (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];
..unrelated code..

如果有人能解决这个问题,那将不胜感激。
杰克
编辑后:在 m_DatabasePath 路径上对数据库运行 select 语句,它返回了空值——文件为空——你有什么想法,为什么我在代码中访问的文件是空的?
第二次编辑:这是我的新代码:
- (void) checkAndCreateDatabase {
    // First, test for existence.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(!success) {

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ProductDatabase.sql"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success) 
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }   
}

- (NSString *) getDBPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"ProductDatabase.sql"];

}

- (void) readProductsFromDatabase {

    NSMutableArray *products = [[NSMutableArray alloc] init];
    self.m_Products = products;
    // The database is stored in the application bundle. 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"ProductDatabase.sql"];
    // Open the database. The database was prepared outside the application.
    if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
        // Get the primary key for all books.
        const char *sql = "SELECT * FROM products";
        sqlite3_stmt *statement;
        // Preparing a statement compiles the SQL query into a byte-code program in the SQLite library.
        // The third parameter is either the length of the SQL string or -1 to read up to the first null terminator.        
        if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
            // We "step" through the results - once for each row.
            while (sqlite3_step(statement) == SQLITE_ROW) {
                //                //Read the data from the results row
                                NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
                                NSString *aCategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
                                NSString *aCalories = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
                                NSString *aFat = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 4)];
                                NSString *aSaturates = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 5)];
                                NSString *aSugar = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 6)];
                                NSString *aFibre = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 7)];
                                NSString *aSalt = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 8)];
                                NSString *aImageURL = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 9)];
                                NSLog(@"Delegate");
                                NSString *aNote = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 10)];
                                NSUInteger myInt = sqlite3_column_int(statement, 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];
            }
        }
        // "Finalize" the statement - releases the resources associated with the statement.
        sqlite3_finalize(statement);
    } else {
        // Even though the open failed, call close to properly clean up resources.
        sqlite3_close(database);
        NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
        // Additional error handling, as appropriate...
    }

}

当我运行这个程序时,出现了错误:
2012-03-22 13:52:29.551 TabbedDietApp[2040:fb03] *** Assertion failure in -[AppDelegate checkAndCreateDatabase], /Users/jacknutkins/Documents/TabbedDietApp/TabbedDietApp/AppDelegate.m:504
2012-03-22 13:52:29.552 TabbedDietApp[2040:fb03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to create writable database file with message 'The operation couldn’t be completed. (Cocoa error 260.)'.'

我对这个问题感到非常头疼。

第三次编辑: 我尝试过:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ProductDatabase" ofType:@"sql"];

但它返回的是nil,我不明白,因为文件在应用程序目录中。


你确定m_DatabasePath路径正确吗?请尝试重置模拟器,进行干净编译并运行应用程序,然后记录下dB路径的NSLog输出。 - Praveen-K
@Praveen-K 是的,我进行了清理和构建,NSLog返回:/Users/jacknutkins/Library/Application Support/iPhone Simulator/5.0/Applications/71E460D4-ED11-4C8D-9BDC-AF115CFF50D6/Library/ProductDatabase.sql,但仍然无法工作。 - Jack Nutkins
你能否在终端中使用给定的路径运行sqlite3并在终端中运行查询。 - Praveen-K
我无法在应用程序使用的路径上运行它,但是我可以从我复制它的地方运行它,并且它能够工作。 - Jack Nutkins
那么你的意思是说,当你在终端中运行以下命令时它可以工作: sqllite3 /Users/jacknutkins/Library/Application Support/iPhone Simulator/5.0/Applications/71E460D4-ED11-4C8D-9BDC-AF115CFF50D6/Library/ProductDatabase.sql - Praveen-K
显示剩余2条评论
3个回答

1

新手错误,我没有在“目标成员资格”部分勾选数据库的复选框。感谢大家的时间。


0

我建议您激活一些调试工具:1)在调用copyItemAtPath时添加一个NSError参数。2)将sqlite3_prepare_v2(l_Database,sqlStatement,-1,&compiledStatement,NULL)语句移到自己的行,并检查返回值。如果您没有正确地复制文件,则打开将为您提供一个空白的DB,这似乎是您遇到的问题。

NSError * error;
BOOL copiedIt = [l_FileManager copyItemAtPath:l_DatabasePathFromApp toPath:m_DatabasePath error:&error];

以上至少可以告诉你它是否起作用。如果不起作用,你就知道问题出在哪里了。


0

“select *from products” 应该改为 “select * from products” 吧?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接