核心数据SQLite文件无数据

时间:2015-06-15 05:32:52

标签: ios sqlite core-data

当我调试Core Data时,我从iPhone中的应用程序中获取了SQLite文件。除了版本之外,SQLite没有数据。我确信我已使用NSManagedObject保存了数据,并且我还可以通过代码获取数据。我甚至可以重新打开应用程序。日志显示数据已插入SQLite文件。

以下显示第一次将数据持久保存到SQLite文件:

- (void)write
{

  NSEntityDescription *entity = [NSEntityDescription entityForName:NSStringFromClass([Network class]) inManagedObjectContext: [AppDelegate sharedAppDelegate].managedObjectContext];

  Network *network = (Network*)[[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];
  network.name = @"network";
  [self.managedObjectContext save:nil];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  [self write];

  return YES;
}

以下是调试核心数据的日志:

2015-06-15 13:10:28.198 SmartLight[1226:419855] CoreData: annotation: Connecting to sqlite database file at "/var/mobile/Containers/Data/Application/FB384AE0-5DC5-4924-8F58-0B944AD63F03/Documents/LightModel.sqlite"
2015-06-15 13:10:28.204 SmartLight[1226:419855] CoreData: annotation: creating schema.
2015-06-15 13:10:28.205 SmartLight[1226:419855] CoreData: sql: pragma page_size=4096
2015-06-15 13:10:28.206 SmartLight[1226:419855] CoreData: sql: pragma auto_vacuum=2
2015-06-15 13:10:28.219 SmartLight[1226:419855] CoreData: sql: BEGIN EXCLUSIVE
2015-06-15 13:10:28.220 SmartLight[1226:419855] CoreData: sql: SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'
2015-06-15 13:10:28.222 SmartLight[1226:419855] CoreData: sql: CREATE TABLE ZBLUEDEVICE ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ZDEVICEID INTEGER, ZDEVICETYPE INTEGER, ZMAXDEVICEID INTEGER, ZNETWORKID INTEGER, ZNETWORK INTEGER, Z4REMOTERS INTEGER, Z4LIGHTS INTEGER, ZADDRESS VARCHAR, ZNAME VARCHAR ) 
2015-06-15 13:10:28.226 SmartLight[1226:419855] CoreData: sql: CREATE INDEX ZBLUEDEVICE_ZNETWORK_INDEX ON ZBLUEDEVICE (ZNETWORK)
2015-06-15 13:10:28.227 SmartLight[1226:419855] CoreData: sql: CREATE INDEX ZBLUEDEVICE_Z4REMOTERS_INDEX ON ZBLUEDEVICE (Z4REMOTERS)
2015-06-15 13:10:28.229 SmartLight[1226:419855] CoreData: sql: CREATE INDEX ZBLUEDEVICE_Z4LIGHTS_INDEX ON ZBLUEDEVICE (Z4LIGHTS)
2015-06-15 13:10:28.230 SmartLight[1226:419855] CoreData: sql: CREATE INDEX ZBLUEDEVICE_Z_ENT_INDEX ON ZBLUEDEVICE (Z_ENT)
2015-06-15 13:10:28.231 SmartLight[1226:419855] CoreData: sql: CREATE TABLE ZNETWORK ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ZMAXDEVICEID INTEGER, ZNETWORKID INTEGER, ZNAME VARCHAR, ZPASSWORD VARCHAR ) 
2015-06-15 13:10:28.232 SmartLight[1226:419855] CoreData: annotation: Creating primary key table.
2015-06-15 13:10:28.233 SmartLight[1226:419855] CoreData: sql: CREATE TABLE Z_PRIMARYKEY (Z_ENT INTEGER PRIMARY KEY, Z_NAME VARCHAR, Z_SUPER INTEGER, Z_MAX INTEGER)
2015-06-15 13:10:28.234 SmartLight[1226:419855] CoreData: sql: INSERT INTO Z_PRIMARYKEY(Z_ENT, Z_NAME, Z_SUPER, Z_MAX) VALUES(1, 'BlueDevice', 0, 0)
2015-06-15 13:10:28.235 SmartLight[1226:419855] CoreData: sql: INSERT INTO Z_PRIMARYKEY(Z_ENT, Z_NAME, Z_SUPER, Z_MAX) VALUES(2, 'Light', 1, 0)
2015-06-15 13:10:28.236 SmartLight[1226:419855] CoreData: sql: INSERT INTO Z_PRIMARYKEY(Z_ENT, Z_NAME, Z_SUPER, Z_MAX) VALUES(3, 'Remoter', 1, 0)
2015-06-15 13:10:28.236 SmartLight[1226:419855] CoreData: sql: INSERT INTO Z_PRIMARYKEY(Z_ENT, Z_NAME, Z_SUPER, Z_MAX) VALUES(4, 'Network', 0, 0)
2015-06-15 13:10:28.237 SmartLight[1226:419855] CoreData: sql: CREATE TABLE Z_METADATA (Z_VERSION INTEGER PRIMARY KEY, Z_UUID VARCHAR(255), Z_PLIST BLOB)
2015-06-15 13:10:28.239 SmartLight[1226:419855] CoreData: sql: SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'
2015-06-15 13:10:28.240 SmartLight[1226:419855] CoreData: sql: DELETE FROM Z_METADATA WHERE Z_VERSION = ?
2015-06-15 13:10:28.241 SmartLight[1226:419855] CoreData: sql: INSERT INTO Z_METADATA (Z_VERSION, Z_UUID, Z_PLIST) VALUES (?, ?, ?)
2015-06-15 13:10:28.242 SmartLight[1226:419855] CoreData: sql: COMMIT
2015-06-15 13:10:28.256 SmartLight[1226:419855] CoreData: sql: pragma journal_mode=wal
2015-06-15 13:10:28.271 SmartLight[1226:419855] CoreData: sql: pragma journal_mode=wal
2015-06-15 13:10:28.272 SmartLight[1226:419855] CoreData: sql: pragma cache_size=200
2015-06-15 13:10:28.273 SmartLight[1226:419855] CoreData: sql: SELECT Z_VERSION, Z_UUID, Z_PLIST FROM Z_METADATA
2015-06-15 13:10:32.569 SmartLight[1226:419855] CoreData: sql: BEGIN EXCLUSIVE
2015-06-15 13:10:32.570 SmartLight[1226:419855] CoreData: sql: SELECT Z_MAX FROM Z_PRIMARYKEY WHERE Z_ENT = ?
2015-06-15 13:10:32.571 SmartLight[1226:419855] CoreData: sql: UPDATE Z_PRIMARYKEY SET Z_MAX = ? WHERE Z_ENT = ? AND Z_MAX = ?
**2015-06-15 13:10:32.572 SmartLight[1226:419855] CoreData: sql: COMMIT
2015-06-15 13:10:32.578 SmartLight[1226:419855] CoreData: sql: BEGIN EXCLUSIVE
2015-06-15 13:10:32.580 SmartLight[1226:419855] CoreData: sql: INSERT INTO ZNETWORK(Z_PK, Z_ENT, Z_OPT, ZMAXDEVICEID, ZNAME, ZNETWORKID, ZPASSWORD) VALUES(?, ?, ?, ?, ?, ?, ?)
2015-06-15 13:10:32.581 SmartLight[1226:419855] CoreData: sql: COMMIT**
2015-06-15 13:10:32.584 SmartLight[1226:419855] CoreData: sql: pragma page_count
2015-06-15 13:10:32.585 SmartLight[1226:419855] CoreData: annotation: sql execution time: 0.0007s
2015-06-15 13:10:32.585 SmartLight[1226:419855] CoreData: sql: pragma freelist_count
2015-06-15 13:10:32.586 SmartLight[1226:419855] CoreData: annotation: sql execution time: 0.0005s

日志显示已插入网络列。

以下是应用关闭并再次打开后来自Core Data的读取数据:

- (void)read
{
  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([Network class])];
  Network *network = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil].firstObject;
  NSLog(@"%@", network);
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  [self read];

  return YES;
}

日志响应显示:

    2015-06-15 13:16:12.344 SmartLight[1234:421137] CoreData: annotation: Connecting to sqlite database file at "/var/mobile/Containers/Data/Application/7A74B12A-6481-4B10-9307-FC7A92DD1912/Documents/LightModel.sqlite"
2015-06-15 13:16:12.347 SmartLight[1234:421137] CoreData: sql: SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'
2015-06-15 13:16:12.350 SmartLight[1234:421137] CoreData: sql: pragma journal_mode=wal
2015-06-15 13:16:12.350 SmartLight[1234:421137] CoreData: sql: pragma cache_size=200
2015-06-15 13:16:12.351 SmartLight[1234:421137] CoreData: sql: SELECT Z_VERSION, Z_UUID, Z_PLIST FROM Z_METADATA
2015-06-15 13:16:12.352 SmartLight[1234:421137] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZMAXDEVICEID, t0.ZNAME, t0.ZNETWORKID, t0.ZPASSWORD FROM ZNETWORK t0 
2015-06-15 13:16:12.353 SmartLight[1234:421137] CoreData: annotation: sql connection fetch time: 0.0004s
2015-06-15 13:16:12.353 SmartLight[1234:421137] CoreData: annotation: total fetch execution time: 0.0010s for 1 rows.
2015-06-15 13:16:20.603 SmartLight[1234:421137] <Network: 0x17015bf90> (entity: Network; id: 0xd000000000040000 <x-coredata://500A532B-CA76-48FB-B0C2-5FE0AFFAB454/Network/p1> ; data: <fault>)

日志显示数据已成功保存。但是,Documents目录中的SQLite文件找不到我保存的数据,系统保存的数据除外(例如版本)。

enter image description here

ZNETWORK表应该有一列。

任何人都可以解释发生了什么,或者为什么我在SQLite文件中找不到数据?

2 个答案:

答案 0 :(得分:2)

我知道会发生什么。这三个文件必须放在一起。enter image description here

答案 1 :(得分:0)

是的,这是正确的。在打开SQLite文件之前,需要将3个文件放在相同的类别中,否则它将创建空白的帮助程序文件。这次重做您的工作流程,将所有3个文件复制到同一目录中,然后打开。