Sqlite数据库加载失败 - sqlite prepare语句问题 - iPhone - xCode 4.3.1 [英] Sqlite Database Load Fails - Issue with sqlite prepare statement - iPhone - xCode 4.3.1

查看:240
本文介绍了Sqlite数据库加载失败 - sqlite prepare语句问题 - iPhone - xCode 4.3.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having issues with the following code which loads an SQLite database.

- (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,这可能是问题吗?

From using breakpoints I can see that the problem is with the if statement and that the code never gets past this if statement. Can anyone spot what might be wrong ? The code was working a few months ago and I have recently upgraded to xCode 4.3 so might this be the problem ?

提前感谢。

推荐答案

是的,我同意Joachim。有一个问题,有时DB并不真正连接。我做的是几件事情。首先,我在应用程序应用程序代理中添加以下代码。

Yeah i agree With Joachim. there is a problem sometimes the DB doesnot really connect. what i do is a couple of things. First i add following Code in my Application App Delegate.

- (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中调用此函数。

Call This Function in ApplicationDidFinishLaunching.

现在删除当前在ur包中的数据库。 (确认它的备份)。和(如果可能删除所有项目从Iphone模拟器文件夹)Coz有时,以前的数据库附加。
清理您的项目,在ur包中添加数据库。

Now remove the data base that is in ur bundle currently. (MAKE SURE U HAD BACKUP OF IT). And (if Possible Delete All the project From Iphone Simulator Folder) Coz sometimes the previous Database is attached. Clean Your project, Add the Data Base in ur bundle. Compile it..

让我知道是否有效

获取路径函数

- (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"];
}

这篇关于Sqlite数据库加载失败 - sqlite prepare语句问题 - iPhone - xCode 4.3.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆