iOS Data Storage with SQLite

iOS Data Storage with SQLite

Introduction

Applications on the App store, which have to deal with storing and managing large amounts of data will prefer the use of a database. When the question of which is the most preferred database that apps in iOS would opt for comes up, the most likely answer would be SQLite, a relational database. iOS with SQLite is one of the most powerful combinations for building data persistent iOS mobile applications.It is contained in a C-library and is embedded in the app that is about to use it. Apple’s iOS SDK provides native support for SQLite database. In this blog we will explain the process of creating a simple iOS application with SQLite data storage and retrieval process.

Use Case

This use case deals with the steps involved in developing an iOS application with a SQLite database, for storing Employee details(Name, Designation, Employee Number, DOB) and later for retrieval of the same, and finally displaying the output on the screen. What we need to do?

  • Prerequisites
  • Create a new project via Xcode.
  • Create, insert and retrieve Employee details, and finally Display the Output.

Solution

Prerequisites

  • Download and Install Mavericks Version 10.9.4 OS X:

http://www.apple.com/osx/

  • Download and Install Xcode 6:

https://developer.apple.com/xcode/downloads/

  • Add libsqlite3.0.dylib

Creating a new project

The necessary steps for creating a new project are as follows:

Step 1: Select the “Single View Application” template, in the Application category under the iOS section. Click on the Next button to proceed:

single_view_app

Step 2: Now, create the new project in Xcode. Specify the options given below, then click on the Next button to proceed further:

project_options

Step 3: Add libsqlite3.0.dylib library into the newly created project.

library

Step 4: Create the following Objective C classes, which will appear as shown in the below screen:

  • ViewController.h
  • Editor.h
  • DBManager.h

objectives

Step 5: Whatever we want can be designed here (story board, Xib,etc). Here below, we have designed Employee details text input fields and action buttons, that are connected with separate ViewControllers’ class files.

view_controller

Step 6: Open DBManager.h file and execute the below code to create database file and table.

Step 7: Open DBManager.m file and implement the below code to construct path to the database file employee.db.

Step 8: Open Editor Class file and use the below code. In Editor Class, some input details have been created, with data storage action code.

Step 9: Open ViewController class file and use the below given code. In the ViewController class file, the process for finding the employee details is given as follows:

Output

When we finally click on the run button to launch the application, we can view the following output on the mobile screen:

application_output

Conclusion

In this blog we have discussed about iOS application processing with SQLite as the data storage and retrieval option. SQLite being lightweight, self-contained, cross-platform, and a relational DBMS has been considered the most suitable storage solution for the mobile platform. We can basically store and process data using six custom data handling methods in iOS. In our forthcoming blog in this category, we will discuss each of these methods in detail.

References

3875 Views 1 Views Today