Adding data using SQLite for iPhone

sqlite_editor_banner2

This is the Sqlite database app. Some times your app may have the need to store data in some kind of database. In iPhone application you can do so using SQLite.

Step 1: Open the Xcode and create a new Xcode project using Navigation base application template. Give the application name “SQL”. As shown in the figure below:

Step 2: Expand classes and notice Interface Builder created the RootViewController.h and RootViewController.m class for you. Expand Resources and notice the template generated a separate nib, RootViewController.xib.

Step 3: To create database in sqlite follow the below steps:
Now that the terminal is open let’s create the database. This is done with the command:

 

SQLite3 will now start and load the Datalist.sqlite database. By default the database is empty and contains no tables.
We only need to create one table. We will create a table called Datalist by typing the following statement:

 

One thing to note here is the pk field. It is the primary key of the table.
Now that our table has been created, let’s add some data. Type the following commands below.

 

Your terminal window will look something as shown below:

Now go back to XCode. Do a Control-Click (right click) on the folder named Resources. Click Add -> Existing Files… and browse to your Datalist.sqlite file and click Add. It will then prompt you with a screen as shown below:

Step 4: Now that we have added the database, we need to load the Objective C libraries so we can use it. Do a control-click (right click) on the Frameworks folder. Click Add -> Existing Frameworks. So in the search bar type in libsqlite3. The file we are looking for is called libsqlite3.0.dylib.

Step 5: We need to add another file. Right-click on the Classes folder and choose Add -> New File. Under Cocoa Touch Class category choose Objective-C class. Name it SqlA.h and SqlA.m file.
This will be a very simple class that will take our dummy data file, read it in as an NSArray and provide some utility methods to access the data from the file.

 

 

Step 6: Open the SqlA.m file and make the following changes in the file.

 

 

Step 7: Open the RootViewController.h file and make the following changes in the file.

 

 

Step 8: Open the RootViewController.m file and make the following changes in the file.

 

 

Step 9: Open the SQLAppDelegate.h file and make the following changes in the file.

 

 

Step 10: Open the SQLAppDelegate.m file and make the following changes in the file.

 

 

Step 11: Now compile and Run the code and get the final output.

You can download source code from here SQL

Comments 3

  1. This walk through does not match xcode 4 as stated above – I tried following this and the latest xcode interface is very different from what is being displayed here. The control right click does not bring up the menus as stated above. Also there is no instruction that says open terminal app to create the SQLite. Might be good idea to divide these posts up into the versions they go with and work with.

  2. is there something missing

    Step 3: To create database in sqlite follow the below steps:
    Now that the terminal is open let’s create the database. This is done with the command:

    follow the steps below then now it is open, how do you open it what are the steps.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">