1

I want to saving entire array into SQLite. I am getting an array having many other arrays in that. Is it possible ? If so please let tell me.

I have tried this - But "ERROR TO SAVE DATA"

NSArray *array = [dict objectForKey:@"events_data"];
 sqlite3_stmt    *statement;

const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
    NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS (event_array) VALUES (\"%@\")", array];

                           const char *insert_stmt = [insertSQL UTF8String];

                           sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
                           if (sqlite3_step(statement) == SQLITE_DONE)
                           {

                               NSLog(@"DATA SAVED TO DB");

                           } else {
                               NSLog(@"ERROR TO SAVE DATA");
                           }
                           sqlite3_finalize(statement);
                           sqlite3_close(contactDB);
                           }

If it is possible please tell me how to retrieve it also.

  • That was a similar topic a week ago or so. To wrap it up, use XML to save your array data. – El Tomato Feb 23 '14 at 14:42
  • If you want the elements of the array inserted as individual rows, using vanilla SQLite, you will need to insert them individually (though it certainly can be done in a relatively compact loop). – Hot Licks Feb 23 '14 at 14:58
  • (Hint: When you get an error in SQLite, dump the value from `sqlite3_errmsg()`. The error message is almost always quite useful.) – Hot Licks Feb 23 '14 at 15:00

1 Answers1

0

You need not use sqlite directly in iOS. Indeed, you should wrap the persistent object in an NSManagedObject. To retrieve the object, simply create an NSFetchRequest, set its entity, and have the context execute it, which will return a standard NSArray of NSManagedObjects, which you can iterate over. Hope this helped, please leave a comment if you have further issues.

hd1
  • 33,938
  • 5
  • 80
  • 91
  • is there any issue using NSUserdefaults. What is the size limit for NSUserdefaults ? – user3343484 Feb 23 '14 at 14:50
  • Doesn't appear [to be a size limit](http://stackoverflow.com/a/7510178/783412), but you can't [persist just any type](http://stackoverflow.com/a/7510246/783412). – hd1 Feb 23 '14 at 15:01