<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

    --- 博客已遷移至: http://www.sealyu.com/blog

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評(píng)論 :: 0 Trackbacks

    I was recently re-writing a yet to be published iPhone app to use Core Data with a pre-populated SQLite database. I was able to get the app to function with a new database using the CoreDataBooks sample code as a reference but could not get the app to read a pre-populated database added to the project.

    After many hours of troubleshooting, I discovered differences in the pre-populated database and a Core Data created SQLite database. The pre-populated database consisted of just my simple 1 table structure and the Core Data created SQLite database consisted of “extra ‘Z’ tables’ For example: Z_METADATA, I searched the Internet and Apple docs but could not find a utility or any other way to convert or prep a pre-populated SQLite database for use with Core Data.

    Right or wrong. Here’s what I finally did (NOTE: All the ‘coding’ is based on the CoreDataBooks sampe):

    In XCode:

    1. Ensure the CoreData framework is added to your project.

    image

    ** If creating a new project, check the ‘Use Core Data for storage’ option when selecting a template.

    image

    2. Create the Data Model file (xcdatamodel) if not already created.

    a. Click File | New File.

    b. Choose iPhone OS | Resource | Data Model

    image

    c. Click Next, name the file and click Next again.

    d. If the model class is already created, select it from   the  next window. You can create it later if it isn’t created already.

    image

    2. Add an Entity (table name) and bind it to the class.

    a. Click Design | Data Model | Add Entity

    b. Rename Entity to the table name you wish to create and change the Class to the desired class name.

    image

    3. Create the Attributes (columns).

    a. Click Design | Data Model | Add Attribute

    b. Rename newAttribute to the desired column name and set the type.

    image

    c. Repeat step b for each additional attribute.

    image

    4. Set the name of the SQLite database name and location in the AppDelegate.m file (Please refer to the CoreDataBooks sample code for further info).

    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
    }
    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @”CoreData.sqlite”];
    /*
    Set up the store.
    For the sake of illustration, provide a pre-populated default store.
    */
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn’t exist, copy the default store.
    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];
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

    NSError *error;
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
    // Update to handle the error appropriately.
    NSLog(@”Unresolved error %@, %@”, error, [error userInfo]);
    exit(-1);  // Fail
    }
    return persistentStoreCoordinator;
    }

    5. Build and Run the app.

    In Finder:

    1. Use Spotlight to search for the SQLite database created by the app in the iPhone Simulator. This is /Users/<Username>/Library/Application Support/iPhone Simulator/User/Application/<Application GUID>/Documents/<database name.sqlite>

    2. Copy that database to a working folder.

    3. Populate that database.

    4. Add the populated database to the project.

    I hope this helps! If anyone has a better or preferred way of formatting a SQLite database for use with Core Data, please let me know.

    posted on 2010-09-29 14:43 seal 閱讀(321) 評(píng)論(0)  編輯  收藏 所屬分類: iPhone
    主站蜘蛛池模板: 亚洲一区二区三区在线观看网站| 亚洲综合色自拍一区| 亚洲精品中文字幕乱码| 三级毛片在线免费观看| 亚洲成人黄色在线| 嫩草成人永久免费观看| 亚洲熟妇av一区二区三区| 国产激情久久久久影院老熟女免费 | 久久久久精品国产亚洲AV无码| 黄网站色视频免费在线观看的a站最新 | 日韩免费一区二区三区在线| 亚洲精品福利视频| 98精品全国免费观看视频| 亚洲高清免费在线观看| 91嫩草国产在线观看免费| 亚洲精品美女网站| 卡1卡2卡3卡4卡5免费视频| 亚洲AV无码之国产精品| 亚洲av日韩av欧v在线天堂| 人人公开免费超级碰碰碰视频| 亚洲午夜无码AV毛片久久| 精品97国产免费人成视频 | 色天使色婷婷在线影院亚洲| 日本无吗免费一二区| 羞羞漫画页面免费入口欢迎你| 亚洲国产成人精品久久久国产成人一区二区三区综| 久久人午夜亚洲精品无码区| 亚洲精品无码激情AV| 国产日韩一区二区三免费高清| 久久精品国产亚洲AV香蕉| 国产一卡2卡3卡4卡2021免费观看 国产一卡2卡3卡4卡无卡免费视频 | 青青视频观看免费99| 亚洲精品无码专区在线播放| www国产亚洲精品久久久日本| 国内精品免费久久影院| 亚洲白嫩在线观看| 免费夜色污私人影院在线观看| 中文字幕免费人成乱码中国| 亚洲国产精品日韩在线| 在线a亚洲v天堂网2018| 特级无码毛片免费视频尤物|