开发者

iOS, SQLite: can't read from the DB

开发者 https://www.devze.com 2023-04-04 07:18 出处:网络
I have an SQLite database, I can open it with SQLite browser, execute queries to it etc. But I can\'t get any data from it in my Xcode project.

I have an SQLite database, I can open it with SQLite browser, execute queries to it etc. But I can't get any data from it in my Xcode project.

NSString *databaseName = @"dbFile.db";
NSArray *documentPaths = NSSearchPath开发者_Python百科ForDirectoriesInDomains(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);
}

The very same query executed from SQLite browser for Mac works just fine.


sqlite3_open doesn't report all errors until you start accessing the data. For example just about any filename and path will pass without errors.

Are you sure the database is in the Documents folder? It's not there by default. Perhaps it's in the main bundle?

Try NSString *databaseName = [[NSBundle mainBundle] pathForResource:@"dbFile" ofType:@"db"];

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号