简体   繁体   中英

How to open CoreData database file to see saved values

Right now i am using coredata to save my data. Its all working fine but now i changed the logic to save values to the database. So i need to compare that the after logic change same values are getting saved in tables. So I need to compare tables.

I went through many links like link 1 link 2

all the links shows that the database file that coredata create is of .sqlite extension. But the files create at that location are "persistentStore, persistentStore-shm, persistentStore-wal" as show in screenshot.

数据库文件的图像

How should i suppose to open these files to see the saved data in tables. Thanks in advance

 - (void)setupDatabase:(void (^)(BOOL))completionHandler
{

    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"MainDataModel"];

    self.db = [[CWUIManagedDocument alloc] initWithFileURL:url];

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                                  [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                                  [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

    self.db.persistentStoreOptions = options;

    if(![[NSFileManager defaultManager] fileExistsAtPath:[self.db.fileURL path]])
    {
        [self.db saveToURL:self.db.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            self.dataManager.db = self.db;
            completionHandler(success);
        }];
    } else if (self.db.documentState == UIDocumentStateClosed) {
        [self.db openWithCompletionHandler:^(BOOL success) {
            self.dataManager.db = self.db;
            completionHandler(success);
        }];
    }
}

Your sql file is the one without extension. The other 2 files are for journaling mode.

This has already been explained in this question . Apple changed the default journaling mode to WAL on iOS 7.

In setupDatabase() method replace the following line

 url = [url URLByAppendingPathComponent:@"MainDataModel"]

with

 url = [url URLByAppendingPathComponent:@"MainDataModel.sqlite"]

You have to create the persistent store type as Sqlite

Run these commands on terminal and open your persistentStore in sqlite manager. These commands will merge the WAL file into the main sqlite file

$ sqlite3 persistentStore
sqlite> PRAGMA wal_checkpoint;

Press control + d

These steps are already covered in the answer of the above link : https://stackoverflow.com/a/43406516/468724

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