Before we can create a database, we need to import the SQLite framework at the top of the screen. Now, declare a variable for sqlite3
and create a NSString
property to store the database path. Within the main Xcode project navigator, select the DatabaseViewController.h
file and modify it as follows:
Now it's time to design our user interface for the SQLite iPhone application. Select the storyboard and then drag and drop the table view to the view along with the table view cell. For tableviewcell
, select the Subtitle style from the attribute and give it an identifier (for example, cell); and add one button on the navigation bar (style -> add
). Make an outlet connection of the table view and button to ViewController.h
.
The final screen should look like the following screenshot:
Now we will have to connect the table view outlet with the code. For that, press Ctrl and drag the tableView
object to the ViewController.h
file. Once connected, you should be able to see the connection in the dialog box with an establish outlet connection named tableView
. Repeat the steps to establish the action connections for all the other UI components in view.
Once the connections are established for all, on completion of these steps, the ViewController.h
file should read as follows:
Now we need to check whether the database file already exists or not. If it is not there, then we need to create the database, path, and table. To accomplish this, we need to write some code in our viewDidLoad
method. So go to the ViewController.m
file and modify the viewDidLoad
method as follows:
The code in the preceding method performs the following tasks:
First, we identify the paths available in our directory and store them in an array (dirPaths
). Then, we create the instance of NSFileManager
and use it to detect whether the database has been created or not. If the file has not been created, then we create the database via a call to the SQLite sqlite3_open()
function and create the table as well. And at last, we close the database.
Create a new class named SecondViewController
. Go to the storyboard and drag one view controller to the canvas. Design the UI according to the following screenshot:
Now make an outlet and action connection for this.
Create some properties in SecondViewController.h
:
In addition, to save the data first, we need to check whether the database is open or not; if it is open, then write a query to insert the data in our table. After writing the data in our table, clear the text present in the text fields. At last, close the database as well.
In order to implement this behavior, we need to modify the save
method:
Now we want to populate this saved data in tableview
. To achieve this task, go to ViewController.m
and create one mutable array using the following line of code and initialize it in viewDidLoad
. This array is used to save our SQLite data:
In the same class, add the following code to the action button to push the view to new view:
Create the v
iewWillAppear
method in the ViewController
class and modify it as in the following code:
In the preceding code, first we open SQLite and then we fire a query SELECT name, designation FROM EMPLOYEE
through which we can get all the values from the database. After that, we will make a loop to store the values one by one in our mutable array (dataFromSQL
). After storing all the values, we will reload the tableview
.
Modify the tableview
data source and the delegate method as follows, in ViewController.m
:
The final step is to build and run the application. Feed the contact details in the second page of the application:
Now click on the
Save button to save your contact details to the database.
Now, when you go back to the Employee List view, you will see the newly saved data in your table view: