简体   繁体   中英

ios - open sqlite database

I am trying to read data from a sqlite database and display it in a table view in an ios app. But I am not able to open the sqlite database for some reason and the app crashes with the message - Failed to open database.

I created the database using the Firefox sqlite manager and copied the file into the Xcode project.

Here's my code -

-(NSString *)dataFilePath{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:kFilename];
}

I am trying to open the database using the sqlite3_open_v2 command inside the viewDidLoad

sqlite3 *database;

    if (sqlite3_open_v2([[self dataFilePath] UTF8String], &database, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
        sqlite3_close(database); // not sure you need to close if the open failed
        NSAssert(0, @"Failed to open database");
    }

This is how I open my database. I have not been doing SQLite for a long time and I am not able to give you much help, other than providing this code that I use in my own application. This is from a class that I use in my app.

static sqlite3 *database;
static sqlite3_stmt *enableForeignKey;

@implementation DBAdapter

+ (sqlite3 *)sharedInstance {

    if (database == NULL) {
        sqlite3 *newDBconnection;

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"klb_db.sqlite"];

        if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {   
            //NSLog(@"Database Successfully Opened :)");
            database = newDBconnection;

            if (sqlite3_prepare_v2(database, "PRAGMA foreign_keys = ON", -1, &enableForeignKey, NULL) != SQLITE_OK) {
                NSLog(@"ERROR IN PRAGMA!");
            }

            sqlite3_finalize(enableForeignKey);

        } else {
            NSLog(@"Error in opening database :(");
            database = NULL;
        }
    }

    return database;
}

Am I correct in assuming that your question was answered during our discussion in the comments of ios - sqlite prepare statement ? Namely, we want to make sure that the database is included in the bundle (by confirming the "Copy Bundle Resources" setting in Xcode for your target's "Build Phases") and that you want want your app to check for the existence of the database in your documents folder and if it's not present, you'll copy it from your bundle ( [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:kFilename] ) to your documents folder.

This may be beyond the scope of your original question, but as you contemplate the eventual deployment of your app, and more importantly, subsequent upgrades/releases of your app, you might want to add some table to your database with some internal database version number that you can use to determine what version of the database the user has in their documents folder, and do any modifications to that database that you might need to support the latest versions of your app. Just a thought.

Anyway, if you still have any open questions, let us know. Otherwise I'll assume we've resolved your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM