无法从控制器调用sqlite查询

时间:2009-09-29 09:16:54

标签: iphone objective-c xcode

我有一段时间有问题无法解决:

在applicationDidFinishLaunching中我有这段代码:

[self checkAndCreateDatabase];
[self readCharsFromDatabase];// which stores into an array some objects
[self readGlyphsFromDatabaseAtId:@"a"];// idem

第二个数组我正在使用辅助ViewController,我在viewDidLoad中获取数组:

AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
self.array = delegate.array2;

直到现在所有完美,只是我想在获得array2之前运行一个新查询。我正在尝试这个:

AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate readGlyphsFromDatabaseAtId:@"b"];// which is supposed to override my array2 with new values
self.array = delegate.array2;

这会停止我的应用程序而不会出错。我只收到此消息:

GNU gdb 6.3.50-20050815(Apple版gdb-966)(2009年3月10日星期二02:43:13) 版权所有2004 Free Software Foundation,Inc。 GDB是免费软件,由GNU通用公共许可证涵盖,您就是 欢迎在某些条件下更改和/或分发它的副本。 输入“show copying”查看条件。 GDB完全没有保修。输入“show warranty”了解详情。 此GDB配置为“i386-apple-darwin”.sharedlibrary apply-load-rules all 附加到流程4736。

我的方法被调用到这一点,之后我没有在 if 中获得NSLog,在 else 中也没有:

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)

有人可以帮忙吗?或者,如果逻辑错误指向我正确的方法吗? 如果内存不大,我可以将所有数据库存储在某些数组中,它有400kb 谢谢!

编辑:这就是我的功能:

-(void) readGlyphsFromDatabaseAtId:(NSString *)charId {
    NSLog(@"reading glyphs for id %@", charId);
    // Setup the database object
    sqlite3 *database;

    // Init the animals Array
    glyphs = [[NSMutableArray alloc] init];
    NSLog(@"1");
    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        NSLog(@"reading row");
        // Setup the SQL Statement and compile it for faster access
        NSString *query = [NSString stringWithFormat:@"SELECT nr,title,description FROM `glyphs` WHERE `id`='%@'", charId];
        NSLog(@"%@", query);
        //const char *sqlStatement = "SELECT nr,title,description FROM `glyphs` WHERE `id`='b'";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, [query UTF8String], -1, &compiledStatement, nil) == SQLITE_OK) {
            NSLog(@"prepared OK");
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                //NSLog(@"reading row");
                NSString *aNr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                NSString *aTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSString *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                //NSLog(@"glyph: %@ %@ %@", aNr, aTitle, aDescription);

                Glyph *g = [[Glyph alloc] initWithCharId:charId glyphNr:aNr glyphTitle:aTitle glyphDescription:aDescription];

                [glyphs addObject:g];
                [g release];
            }
        }
        else {
            NSLog(@"not prepared");
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);
    }
    else{
        NSLog(@"sqlite not ok");
    }
    sqlite3_close(database);
    NSLog(@"fin reading database2");
}

2 个答案:

答案 0 :(得分:1)

好吧,似乎数据库路径无法访问,所以我重新启用它,现在可以工作了。如果有人可以启发一下为什么......

databasePath包含在applicationDidFinishLaunching

答案 1 :(得分:0)

您是否在sqlite3_open()的每次调用中调用readGlyphsFromDatabaseAtId?如果您在数据库已经打开的情况下这样做,可能会导致问题。