2011-06-28 22 views
7

Tôi vừa mới bắt đầu sử dụng dữ liệu cốt lõi. Tôi muốn thiết lập một db được điền trước. Tôi đọc ở đâu đó rằng dữ liệu lõi tạo ra một tập tin sqlite khi một ứng dụng dữ liệu cốt lõi được chạy. Tôi không biết nơi để tìm nó mặc dù.XCode4 - nơi tìm tệp sqlite được tạo bởi dữ liệu lõi

Tôi đã làm theo hướng dẫn trên blog nhưng không tìm thấy tệp sqlite trên thư mục được chỉ định vị trí /Users/<Username>/Library/Application Support/iPhone Simulator/User/Application/<Application GUID>/Documents/<database name.sqlite> cũng như trong thư mục ứng dụng.

đây là mã của tôi cho persistentCoordinator.

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (__persistentStoreCoordinator != nil) 
    { 
     return __persistentStoreCoordinator; 
    } 

    NSString *storePath = [[[self applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"coredata.sqlite"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if (![fileManager fileExistsAtPath:storePath]) { 
     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"coredata" ofType:@"sqlite"]; 
     if (defaultStorePath) { 
      [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; 
     } 
    } 

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

    NSError *error = nil; 
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) 
    { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    }  

    return __persistentStoreCoordinator; 
} 

Trả lời

16

Những file đó là trong ~/Library/Application Support/iPhone Simulator/[SDK version]/Applications/[App GUID]/Documents đối với tôi, cả hai cho Xcode 3 và Xcode 4.

Nếu bạn có nhiều SDK, hãy chắc chắn để tìm trong tất cả các thư mục phiên bản SDK khác nhau cho ứng dụng của bạn.

+0

Cách biết ** App GUID **? –

1

Đối với tôi đó là trong whatMuregSaid/[Ứng dụng Guid]/Library/Application Support/[AppName] /filename.sqlite

0

hy vọng điều này sẽ giúp bạn ít nhiều

  1. /Users/Tên đăng nhập/sau đó nhấn cmd + shift + G và viết/Users/Username/Library, Bây giờ bạn sẽ thấy thư mục Library sau khi nó đi đến Application Support/iPhone Simulator/7.1 (hoặc 7.1-64)/Applications/F84D4CC8-326E-4A2E-8A37 -F1A755D6FCC4/Tài liệu bạn sẽ thấy ba tệp và một tệp .sqlite.

  2. Để xem cấu trúc và thông tin khác của tệp sqlite này, cách hiệu quả nhất là sử dụng trình quản lý SQLITE (thêm vào firefox https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/) thêm nó và sau đó nhấp vào công cụ.

1

Thực hiện theo các bước sau:

  1. Trong chức năng applicationDocumentsDirectory của bạn trong AppDelegate.swift thêm mã này:

    let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)

    // Look for this in the console, this will print out the location of the sqlite database, which you can then open with an SQLite viewer

    print("urls.last \(urls.last)")

  2. Look cho điều này trong giao diện điều khiển, điều này sẽ in ra vị trí của cơ sở dữ liệu SQLite, mở thiết bị đầu cuối của bạn và open whatever_the_location_path_was

  3. Tải SQLite xem http://sqlitebrowser.org/

  4. Nhấp chuột phải vào tập tin yourapp.sqlite trong thư mục, chọn Mở Với sqlitebrowser mà bạn đã tải xuống

Chúc mừng!

Các vấn đề liên quan