Sqlite Database part 2 : Copying the database file from project bundle to document directory

Before reading this part kindly have a look at my first post for sqlite here.



To handle the database there is an in-build library that comes with xcode which is called as libsqlite3.0dylib and if you are working with any database oriented project then in that case you need to first add this library into your project. 

To use this library you have to use #import<sqlite3.h> in your header file. 





Steps to add this library is similar to the way you add a framework into your project.




In this post we will first create a class which will help us in performing the database operations. I will be adding an employee name and his corresponding department with his image, user may select the image from the iOS gallery by tapping on the image. 

The main focus of this post will be database oriented only hence i wont be showing you the UI part which you may see by downloading the source code and in case if you are facing any issues with the UI part feel free to ask me via mails or via comments.

I have created an NSObject subclass named DatabaseHelper where i have wrote all the code for handling the database related operations, given below is an image displaying the list of functions in the DatabaseHelper.h file





So as you may see from the above image the functions that i have declared that will assist me with data insertion with sqlite, the description about the function is mentioned as comments so that you can have a proper understanding regarding its role in the play.

Note: The database file present in your project bundle is just a copy from which you may read the data but not write new data into it, since its read only and hence inorder to add data in our sqlite file we need to copy this file (sqlite file) in the applications document directory from where we can not only read the data but as well write new data into it. 

Hence before working with sqlite database you have to copy this file in the document directory of your application and then begin with reading or adding of data in the database.

Document Directory : This is a folder which is present in every iOS app, this folder contains some files related to your project which only your app can access.

Let's begin with the development process and see how to manage data with sqlite.

Step 1 : The first step is to copy the database file from your bundle or groups and files to the document directory and for that there are three functions that i have wrote which is given below

Function 1 : This function wil help us in copying the database file from the bundle in the doc directory



Code Explanation : First i am checking if my sqlite file is present at document directory and then with the help of NSFileManager i am copying the sqlite file from my project bundle to it's new location i.e. document directory. The function returns boolean indicating whether the database file is copied in the doc directory or has failed in the process.


Function 2 : This function will get the location of the database file from the document directory




Code Explanation : The name of the database that i am using is called as "AdventureWorks.sqlite", documentDirectoryFolderLocation array contains the  path of document directory for your application, and the retuned location from this function will be the new location for our database.



Function 3 : Will check if the db has been copied or if the operation has failed and will print the appropriate message in the Log.






Code Explanation : This function will make a call to the copyDatabaseFromBundletoDocumentDirectory and will return if the copying of the database operation has been success or has failed.


Step 2: After copying the database into the document directory i create a function which will close any open connection to the sqlite database before the execution of any new operation.




Code Explanation : The above code checks if any db connection is open with the help of sqlite3_open function and if a connection is open then it closes the connection with the help of sqlite3_close function which accepts the parameter of databaseReference which is the object of sqlite3.


I hope that you have understood the above steps we will have a look at rest of the steps in our next post where we will start adding and fetching records from the database.

If you have any questions or queries regarding this post then feel free to ask me via comments or emails.

Happy iCoding and have a great day.


Move to the third part of tutorial on sqlite here.


Comments

  1. If possible give the source code of the project.

    ReplyDelete
  2. Thnx Sirji ,,Very Useful for us at every moment we need.

    ReplyDelete
  3. @Bappaditya Dey : Once i finish with my last post on sqlite i shall share the code with you all...

    ReplyDelete
    Replies
    1. @radix : If possible give the source code of the project.

      Delete
  4. @Radix.....Superb man..You did a great job. Just love to read your blog posts.

    Keep it up!

    ReplyDelete
  5. could you please explain about gestures.....

    ReplyDelete
  6. If possible give the source code of the project.

    ReplyDelete
  7. Really Nice Post About Sqlite

    ReplyDelete

Post a Comment