ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How To Backup SQLite Database on iOS

Updated on November 24, 2014
klanguedoc profile image

Kevin is a Software Developer with 20 years experience designing and building business intelligence and system integration solutions.

The essentials of developing iOS apps for iPhone and iPad using SQLite
The essentials of developing iOS apps for iPhone and iPad using SQLite | Source

(c) 2012 kevin languedoc (klanguedoc)

SQLite is a great, lightweight database engine. It has been used extensively in mobile applications and standalone web applications and standalone desktop applications that requires a local storage. SQLite is very fast and compact. It features SQL query language, less the overhead of stored procedures.

SQLite is the defacto standard for iOS applications and it is still used today by Apple. They may have Core Data, but Core Data still needs a storage facility and guess what, it is SQLite. They also use SQLite database in many of their iOS applications like iMail. However all these databases can and will continue to increase in size as users continue to use their apps and store increasingly amounts of data even while iOS devices like Android or BB devices have a limited amount of space, especially when users add music, books, videos and movies. 16 GB will be quickly used up. Even 32 or 64 GB will eventually get used up. As a developer, if you plan on developing production grade applications that uses SQLite, you will need to plan at the onset how you will enable your app to backup the data stored in the database after all it won’t be able to grow forever.

This guide provides some strategies that you can use or build on to implement a data backup strategy for your app.

Interactive versus Automatic

The first decision that needs to be made is whether the backup should be interactive, to let the user the user decide when to backup the database or automatic in the backend without user involvement. Also do you backup the whole database or a certain of the data.

Although there are many ways to make a backup, you have to ensure that you don’t corrupt the database or the existing data. Before commencing a backup, it is best to make a copy of the database to the either the Library or Tmp iOS app directory so if an error occurs the original database can be copied back to its original location.

Next I would make a copy of the original database to the backup location discuss below adding a timestamp to the copied database. Once you get a positive response that the file was successfully copied, you can reduce the size of the database by deleting a part or all of the data depending on the nature of the data.

Alternatively, you could have a backup database at a remote location and move part of the data to this backup database.

In addition to moving the data to a remote location, the choice of the remote location can vary depending on the app’s needs. Some of these choices are offered below.

Archiving

The iOS SDK offers two archiving and 2 unarchiving classes: NSArchiver, NSKeyedArchiver and NSUnarchiver, NSKeyedunarchiver. The NSKeyedArchiver and NSKeyedUnarchiver classes offer the best option since they provide a keyed value to allow access to the different archives. Performance takes a bit of a hit since more identification data is stored with each object. NSKeyedArchiver is a subclass of the NSCoder class.Once the objects are encoded and stored in a file, this file must be stored off site or off device to a secure remote location.

The archivedDataWithRootObject method is a good choice for storing data and returning the data as NSData which can then be streamed to a remote location. Another option is the archiveRootObject:toFile: which not only archives the objects as data but will also write the data to a location defined by the path argument.

Once the archive is created you will need to stored it off the application. You can use one of the following options amongst others.

DropBox

DropBox offers an excellent off site storage option. DropBox SDK offers a RESTful API for iOS so that you integrate your app with DropBox directly using your Objective-C code. Files can be moved in both directions securely. Once stored in DropBox, the files can be moved to corporate networks through integration.

Google Drive

Google Drive is another great off site storage option. Google Drive provides two different APIs that would an iOS application to move files to Google and optionally to a corporate site. You can opt to use the native Objective-C API which requires the addition of the Google Drive iOS Project which must be added to an existing iOS app. The other option is to use the Google Drive RESTful Javascript API. Using this option you can move files to and from Google Drive with NSURLConnection and NSHTTPRequest classes.

Sky Drive

Microsoft’s Sky Drive offers an excellent storage platform, especially if you want to integrate with a corporate network of database server since the .Net Framework offers a native connector for SQLite databases so data can be moved from a SQLite database to a SQL Server for instance.

As the other choices, Sky Drive offers both a Javascript RESTful API and a native Objective-C API for iOS devices. To copy files to Sky Drive, you would need to use the LiveConnectClient copyFromPath:toDestination:delegate:userState method.

iCloud

iCloud is Apple’s off site storage platform. The technology provides the APIs to move file using the file management methods in iCloud. You can also manage file conflicts using NSFileVersion to help you manage the different database files.

Corporate (FTP)

This option uses the native ftp client in iPad which can be setup to work with a public or private ftp site. You can also use the NSURLConnection to establish a ftp connection string which can be used to move database files to and from a corporate network.

Although not listed here, you could move files by converting them to a data format using NSData or CFData and stream the data to another location which is similar to the other methods listed above.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)