fito
fito

Reputation: 43

I can not insert data into sqlite3 file on XCode

I did not find answer that satisfy me, I can insert data into database and I can also select from it on simulator, but when I close the simulator and open it again, I can't see any data. Also I opened it on the terminal and I did not see any data in it. Did I miss any line or is there any logic error?

What did you try? any suggest for me?

I inserted data into my code is like this:

databasePath = [[NSBundle mainBundle] pathForResource:@"persons" ofType:@"sqlite3"];
        const char *dbpath = [databasePath UTF8String];
        sqlite3_stmt    *statement;

        if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
        {
            NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO person (id, name, address, photograph, telephone, fax, district) VALUES (\"%i\",\"%@\", \"%@\", \"%@\",\"%@\",\"%@\",\"%@\")", i, Name,Addresses, Image, Telephone,Fax,District];

            const char *query_stmt = [insertSQL UTF8String];

            sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL);

            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                //  NSLog(@"OK");

            } else {
                //  NSLog(@"Problem!!!");

            }
            sqlite3_finalize(statement);
            sqlite3_close(contactDB); 
        }

Upvotes: 2

Views: 1314

Answers (2)

nick.jw.park
nick.jw.park

Reputation: 199

I was searching for the same answer because I was stuck as well, and I think someone else's post here gives a pretty good example.

Here is the information on the link that Inafziger gave:

iOS Data Storage Guidelines iCloud includes Backup, which automatically backs up a user’s iOS device daily over Wi-Fi. Everything in your app’s home directory is backed up, with the exception of the application bundle itself, the caches directory, and temp directory. Purchased music, apps, books, the Camera Roll, device settings, home screen and app organization, messages, and ringtones are backed up as well. Because backups are done wirelessly and stored in iCloud for each user, it’s best to minimize the amount of data that’s stored for your app. Large files will lengthen the time it takes to perform a backup and consume more of a user’s available iCloud storage.

To ensure that backups are as efficient as possible, be sure to store your app’s data according to the following guidelines:

  1. Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the /Documents directory and will be automatically backed up by iCloud.
  2. Data that can be downloaded again or regenerated should be stored in the /Library/Caches directory. Examples of files you should put in the Caches directory include database cache files and downloadable content, such as that used by magazine, newspaper, and map applications.
  3. Data that is used only temporarily should be stored in the /tmp directory. Although these files are not backed up to iCloud, remember to delete those files when you are done with them so that they do not continue to consume space on the user’s device.
  4. Use the "do not back up" attribute for specifying files that should remain on device, even in low storage situations. Use this attribute with data that can be recreated but needs to persist even in low storage situations for proper functioning of your app or because customers expect it to be available during offline use. This attribute works on marked files regardless of what directory they are in, including the Documents directory. These files will not be purged and will not be included in the user's iCloud or iTunes backup. Because these files do use on-device storage space, your app is responsible for monitoring and purging these files periodically.

Upvotes: 1

lnafziger
lnafziger

Reputation: 25740

You can not make changes to a database in the main bundle, because it is read only.

You must first copy it to a location where you can write to it, such as the users documents or cache directory.

The iOS data storage guidelines specify where you should save it, depending on the type of data.

Upvotes: 1

Related Questions