ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Computer Programming Tutorials

How To Backup SQLite Database on iOS

Updated on November 24, 2014
klanguedoc profile image

Kevin is Software Developer with 20 years experience designing and building software applications including iOS and Android apps.

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.


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 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 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.


    0 of 8192 characters used
    Post Comment

    No comments yet.