【问题标题】:Sqlite Database Load Fails - Issue with sqlite prepare statement - iPhone - xCode 4.3.1SQLite 数据库加载失败 - sqlite prepare 语句出现问题 - iPhone - xCode 4.3.1
【发布时间】:2012-04-10 21:08:31
【问题描述】:

我遇到以下加载 SQLite 数据库的代码的问题。

- (NSArray *)getDatabase {

NSLog(@"Get Database Called");

    NSMutableArray *retval = [[[NSMutableArray alloc] init] autorelease];
    NSString *query = @"SELECT Description, UniqueID, HexCoords, NoHexCoords, NoAnnots, AnnotText, DescriptionFormatting, HexCoordsPortrait FROM MainTable";
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) 
        == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {

            char *nameChars = (char *) sqlite3_column_text(statement, 0);
            char *desChars = (char *) sqlite3_column_text(statement, 1);
            int uniqueID = sqlite3_column_int(statement, 2);

通过使用断点,我可以看出问题在于 if 语句,并且代码永远不会超过这个 if 语句。谁能发现可能出了什么问题?代码几个月前就可以工作了,我最近升级到 xCode 4.3,所以这可能是问题吗?

提前致谢。

【问题讨论】:

  • 我能看到这样做的唯一原因是 _database 设置不正确。打开数据库的调用是否可能失败?此外,最后一个参数应该是NULL,而不是nil,因为该参数不是指向对象的指针。目前两者都编译为 0,因此不会引起问题,但我不知道有任何保证它们将保持相同。

标签: iphone sql database xcode sqlite


【解决方案1】:

是的,我同意 Joachim 的观点。有时数据库没有真正连接会出现问题。我做的是几件事。首先,我在我的应用程序代表中添加以下代码。

- (void) copyDatabaseIfNeeded {

    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(success) {

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

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

在 ApplicationDidFinishLaunching 中调用此函数。

现在删除当前捆绑包中的数据库。 (确保您已备份它)。并且(如果可能的话,从 Iphone Simulator 文件夹中删除所有项目)因为有时会附加以前的数据库。 清理你的项目,在你的包中添加数据库。编译它..

让我知道它是否有效

获取路径函数

- (NSString *) getDBPath {
        //Search for standard documents using NSSearchPathForDirectoriesInDomains
        //First Param = Searching the documents directory
        //Second Param = Searching the Users directory and not the System
        //Expand any tildes and identify home directories.
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex:0];
        return [documentsDir stringByAppendingPathComponent:@"MYDB.sqlite"];
}

【讨论】:

  • 谢谢 - 但是“getDBPath”方法是什么?
  • 抱歉忘了说
  • 我还想添加一件事。在你发布在你的问题中的函数中,你将返回类型设置为 (NSArray *) 这不是最佳实践。你应该使用 NSMutableArray
  • 谢谢 - 这帮助我找到了问题 - 在 Lion 下,我现在似乎需要从实际构建文件夹中删除数据库,清理项目,然后将其拖回项目中。干得好 Apple - 你让 xCode 4.3.1 在这方面比以前更容易出错。
  • 我不认为是 Xcode 的问题。我目前正在使用 Xcode 4.0.2,我遇到了同样的问题。并花半天时间。我很高兴它帮助了你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 2023-03-26
  • 1970-01-01
相关资源
最近更新 更多