简体   繁体   中英

why is there no data in my sqlite file when it is installed on iOS app?

why is there no data in my sqlite file when it is installed on iOS app?

Problem is the app can't see any data in the file/database (it isn't there) when it tries to read from the database.

I have a sqlite file that I am using and is added to the project in Xcode. If I connect to the file/database with a sql editor I see my data. Then I run my app and I use the same editor to read the file on the simulator and there is no data. No tables or rows, but it is the same file name.

I am not doing anything special to copy it to the app or offline folder or anything. Looks like it is going to the Documents folder (that appears to be the default don't know how to change it or if I need to).

You should open the database from the Documents folder, but you have to have your open routine copy it there itself.

  1. Confirm that the database is included in the bundle. Go to your "Target Settings", click on the "Build Phases" tab, and look at the "Copy Bundle Resources" list. Make sure your database is included in there, and add it if it isn't already there.

  2. Your database open routine should:

    • Look for the existence of the database in Documents (using NSFileManager );

    • If not there copy it from the bundle to Documents;

    • Only then, you can try to open the database from Documents (but not from the bundle).

    Note, if you ever tried opening the database from Documents before you finished the above three points, the sqlite3_open command will create a blank database if it doesn't find one. So you might want to make sure you delete any blank databases before trying the above. The easiest way to do that is to delete the app from the device/simulator and then run it again.

Thus, you might have a routine like so:

- (void) installDatabaseIfNeeded {

    // Get the documents database path

    NSString *documentsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *documentsDatabasePath = [documentsFolder stringByAppendingPathComponent:@"test.sqlite"]; // always use setter when setting property's value

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:documentsDatabasePath]) {

        // if the database doesn't exist in documents, look for it in the bundle and copy it if found

        NSString *bundleDatabasePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"sqlite"];

        if (bundleDatabasePath) {
            NSError *error;
            if (![fileManager copyItemAtPath:bundleDatabasePath toPath:documentsDatabasePath error:&error]) {
                NSLog(@"copyItemAtPath failed: %@", error);
            }
        }
    }
}

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