iOS,SQLite:无法从DB读取

时间:2011-09-11 18:00:08

标签: ios sqlite

我有一个SQLite数据库,我可以用SQLite浏览器打开它,对它执行查询等等。但我无法从我的Xcode项目中获取任何数据。

NSString *databaseName = @"dbFile.db";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
sqlite3 *database;

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    NSLog(@"db is ok"); // works just fine
    const char *sqlStatement = "SELECT table.column FROM table";
    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        NSLog(@"OK"); // nothing happens
    }
    sqlite3_close(database);
}

从SQLite浏览器为Mac执行的相同查询工作正常。

1 个答案:

答案 0 :(得分:1)

在您开始访问数据之前,

sqlite3_open不会报告所有错误。例如,几乎任何文件名和路径都会没有错误地传递。

您确定数据库位于Documents文件夹中吗?它默认不存在。 也许它在主捆绑中?

尝试     NSString * databaseName = [[NSBundle mainBundle] pathForResource:@“dbFile”ofType:@“db”];