Iphone Sqlite3数据库表无法在XCode 4.2中打开

Iphone Sqlite3数据库表无法在XCode 4.2中打开,iphone,database,sqlite,Iphone,Database,Sqlite,我正在使用XCode 4.2创建和安装iphone应用程序。我正在使用sqlite3数据库进行应用。当我的XCode 4.2出现问题时,我在iPhone3GS和XCode 3.2.5上成功创建并运行了该应用程序。数据库文件无法打开,以下是打开表的示例代码。当我使用SQlite管理器打开同一个db文件时,我可以看到这个表。我不明白错误是什么 static sqlite3 *database = nil; static sqlite3_stmt *selectStmt = nil; + (void

我正在使用XCode 4.2创建和安装iphone应用程序。我正在使用sqlite3数据库进行应用。当我的XCode 4.2出现问题时,我在iPhone3GS和XCode 3.2.5上成功创建并运行了该应用程序。数据库文件无法打开,以下是打开表的示例代码。当我使用SQlite管理器打开同一个db文件时,我可以看到这个表。我不明白错误是什么

static sqlite3 *database = nil;
static sqlite3_stmt *selectStmt = nil;

+ (void) getInitialDataToDisplay:(NSString *)dbPath {
    NSLog(@"Path: %@",dbPath);
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
        NSString *sqlStr = @"select * from Space";
        const char *sql = [sqlStr UTF8String];
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
            while(sqlite3_step(selectstmt) == SQLITE_ROW) {
                NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
                SpaceClass *spaceObj = [[SpaceClass alloc] initWithPrimaryKey:primaryKey];
                spaceObj.spacePK = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
                spaceObj.spName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
                spaceObj.spDescrptn = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];               
                [appDelegate.spaceArray addObject:spaceObj];
                [spaceObj release];
            }
        }else
            NSLog(@"not ok");
    }
    else
        sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}

请帮忙,谢谢你把关闭方法放错地方了。我已经在iOS中使用SQLite3两周了,我遇到了这个问题。我通过将
SQLite3\u close
方法放在
if(open==ok)
的最后一行解决了这个问题

您的代码应该如下所示:

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{       
    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) 
    {
        while(sqlite3_step(selectstmt) == SQLITE_ROW) 
        {

        }
    } 
    else
    {
        NSLog(@"not ok");
    }
    //here you should close database, before exit from if open block
    sqlite3_close(database);
}
else
{
    //here is not needed because of database open failure
    //sqlite3_close(database);
    NSLog(@"not ok");
}

这应该可以解决您的问题,因为现在每次打开数据库时都要关闭它。但是在你的代码中,你一次又一次地打开它,却没有关闭它

你能检查一下
NSLog(@“Error=%s”,sqlite3_errmsg(&db))给出了什么吗