Records not being saved to core data sqlite file

Posted by esd100 on Stack Overflow See other posts from Stack Overflow or by esd100
Published on 2012-05-28T23:20:52Z Indexed on 2012/06/02 4:40 UTC
Read the original article Hit count: 362

Filed under:

I'm a complete newbie when it comes to iOS programming and much less Core Data. It's rather non-intuitive for me, as I really came into my own with programming with MATLAB, which I guess is more of a 'scripting' language.

At any rate, my problem is that I had no idea what I had to do to create a database for my application. So I read a little bit and thought I had to create a SQL database of my stuff and then import it. Long story short, I created a SQLite db and I want to use the work I have already done to import stuff into my CoreData database.

I tried exporting to comma-delimited files and xml files and reading those in, but I didn't like it and it seemed like an extra step that I shouldn't need to do.

So, I imported the SQLite database into my resources and added the sqlite framework.

I have my core data model setup and it is setting up the SQLite database for the model correctly in the background.

When I run through my program to add objects to my entities, it seems to work and I can even fetch results afterward. However, when I inspect the Core Data Database SQLite file, no records have been saved.

How is it possible for it to fetch results but not save them to the database?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//load in the path for resources
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *databaseName = @"histology.sqlite";
NSString *databasePath = [paths stringByAppendingPathComponent:databaseName];
[self createDatabase:databasePath ];

NSError *error;
if ([[self managedObjectContext] save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);

// Test listing all CELLS from the store
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entityMO = [NSEntityDescription entityForName:@"CELL"
                                            inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entityMO];
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
for (CELL *cellName in fetchedObjects) {
    //NSLog(@"cellName: %@", cellName);

-(void) createDatabase:databasePath {
NSLog(@"The createDatabase function was entered.");  
NSLog(@"The databasePath is %@ ",[databasePath description]);
// Setup the database object
sqlite3 *histoDatabase;

// Open the database from filessytem
if(sqlite3_open([databasePath UTF8String], &histoDatabase) == SQLITE_OK) {
        NSLog(@"The database was opened");
    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "SELECT * FROM CELL";
    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(histoDatabase, sqlStatement, -1, &compiledStatement, NULL) != SQLITE_OK) {
        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(histoDatabase));

    if(sqlite3_prepare_v2(histoDatabase, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

        // Loop through the results and add them to cell MO array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            CELL *cellMO = [NSEntityDescription insertNewObjectForEntityForName:@"CELL" inManagedObjectContext:[self managedObjectContext]];

            if (sqlite3_column_type(compiledStatement, 0) != SQLITE_NULL) {
                cellMO.cellName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
            } else {
                cellMO.cellName = @"undefined";

            if (sqlite3_column_type(compiledStatement, 1) != SQLITE_NULL) {
                cellMO.cellDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
            } else {
                cellMO.cellDescription = @"undefined";

                NSLog(@"The contents of NSString *cellName = %@",[cellMO.cellName   description]);


    // Release the compiled statement from memory


I have a feeling that it has something to do with the timing of opening/closing both of the databases?

Attached I have some SQL debugging output to the terminal

2012-05-28 16:03:39.556 MedPix[34751:fb03] The createDatabase function was entered.

2012-05-28 16:03:39.557 MedPix[34751:fb03] The databasePath is /Users/jack/Library/Application Support/iPhone Simulator/5.1/Applications/A6B2A79D-BA93-4E24-9291-5B7948A3CDF4/ 

2012-05-28 16:03:39.559 MedPix[34751:fb03] The database was opened

2012-05-28 16:03:39.560 MedPix[34751:fb03] The database was prepared

2012-05-28 16:03:39.575 MedPix[34751:fb03] CoreData: annotation: Connecting to sqlite database file at "/Users/jack/Library/Application Support/iPhone Simulator/5.1/Applications/A6B2A79D-BA93-4E24-9291-5B7948A3CDF4/Documents/MedPix.sqlite"

2012-05-28 16:03:39.576 MedPix[34751:fb03] CoreData: annotation: creating schema.

2012-05-28 16:03:39.577 MedPix[34751:fb03] CoreData: sql: pragma page_size=4096

2012-05-28 16:03:39.578 MedPix[34751:fb03] CoreData: sql: pragma auto_vacuum=2

2012-05-28 16:03:39.630 MedPix[34751:fb03] CoreData: sql: BEGIN EXCLUSIVE

2012-05-28 16:03:39.631 MedPix[34751:fb03] CoreData: sql: SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'


2012-05-28 16:03:39.669 MedPix[34751:fb03] CoreData: annotation: Creating primary key table.


2012-05-28 16:03:39.672 MedPix[34751:fb03] CoreData: sql: INSERT INTO Z_PRIMARYKEY(Z_ENT, Z_NAME, Z_SUPER, Z_MAX) VALUES(1, 'CELL', 0, 0)

2012-05-28 16:03:39.701 MedPix[34751:fb03] CoreData: sql: CREATE TABLE Z_METADATA (Z_VERSION INTEGER PRIMARY KEY, Z_UUID VARCHAR(255), Z_PLIST BLOB)

2012-05-28 16:03:39.702 MedPix[34751:fb03] CoreData: sql: SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'

2012-05-28 16:03:39.703 MedPix[34751:fb03] CoreData: sql: DELETE FROM Z_METADATA WHERE Z_VERSION = ?

2012-05-28 16:03:39.704 MedPix[34751:fb03] CoreData: sql: INSERT INTO Z_METADATA (Z_VERSION, Z_UUID, Z_PLIST) VALUES (?, ?, ?)

2012-05-28 16:03:39.705 MedPix[34751:fb03] CoreData: sql: COMMIT

2012-05-28 16:03:39.710 MedPix[34751:fb03] CoreData: sql: pragma cache_size=200

2012-05-28 16:03:39.711 MedPix[34751:fb03] CoreData: sql: SELECT Z_VERSION, Z_UUID, Z_PLIST FROM Z_METADATA

2012-05-28 16:03:39.712 MedPix[34751:fb03] The contents of NSString *cellName = Beta Cell

2012-05-28 16:03:39.712 MedPix[34751:fb03] The contents of NSString *cellName = Gastric Chief Cell


2012-05-28 16:03:39.714 MedPix[34751:fb03] The database was prepared

2012-05-28 16:03:39.764 MedPix[34751:fb03] The createDatabase function has finished. Now fetching.

2012-05-28 16:03:39.765 MedPix[34751:fb03] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZCELLDESCRIPTION, t0.ZCELLNAME FROM ZCELL t0 

2012-05-28 16:03:39.766 MedPix[34751:fb03] CoreData: annotation: sql connection fetch time: 0.0008s

2012-05-28 16:03:39.767 MedPix[34751:fb03] CoreData: annotation: total fetch execution time: 0.0016s for 0 rows.

2012-05-28 16:03:39.768 MedPix[34751:fb03] cellName: <CELL: 0x6bbc120> (entity: CELL; id: 0x6bbc160 <x-coredata:///CELL/t57D10DDD-74E2-474F-97EE-E3BD0FF684DA34> ; data: {
cellDescription = "S cells are cells which release secretin, found in the jejunum and duodenum. They are stimulated by a drop in pH to 4 or below in the small intestine's lumen. The released secretin will increase the s";
cellName = "S Cell";
organs =     (
specimens =     (
systems =     (
tissues =     (

Sections were cut short to abbreviate. But note that the fetch results contain information, but it says that total fetch execution was for "0" rows? How can that be? Any help will be greatly appreciated, especially detailed explanations. :) Thanks.

© Stack Overflow or respective owner

Related posts about database

Related posts about sqlite