How to Develop Ios Database Apps Using Sqlite Tutorials on Developing Iphone and Ipad Database Apps Using Sqlite

How to Develop Ios Database Apps Using Sqlite Tutorials on Developing Iphone and Ipad Database Apps Using Sqlite

How To Develop iOS Database Apps using SQLite Tutorials on developing iPhone and iPad database apps using SQLite Kevin Languedoc This book is for sale at http://leanpub.com/iossqlite This version was published on 2019-02-24 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. © 2014 - 2019 Kevin Languedoc Tweet This Book! Please help Kevin Languedoc by spreading the word about this book on Twitter! The suggested hashtag for this book is ##iossqliteapps. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: ##iossqliteapps To all the programmers who struggle day in, day out to write great iOS apps Contents Updating an iOS SQLite Database .................................... 1 SQLite Queries .............................................. 1 Develop the SQLite iPhone Application ............................... 4 Updating an iOS SQLite Database This chapter is about modifying an existing SQLite database during runtime. It is closely related to the first chapter on creating Databases because I am demonstrating how to add tables, columns, indexes and triggers and not just modifying them. I wanted to group these operations together in their own chapter. SQLite offers some unique capabilities to modify an existing database. What makes SQLite stand out amongst database engines in my opinion is its ability to allow an user to modify a database during runtime which can be very useful. To demonstrate how to modify a database at runtime, I created a sample iOS iPhone application using the Single View template. The UI is very primitive but I wanted to focus on the SQLite queries and how to set them up to be able to modify a database. Another liberty that I am taking is that I have hard coded a lot of new column names, table names, indexes, triggers, and so on that I use to create these new objects in the SQLite database. The objective was not to build a complete database manager app but demonstrate how to setup the queries. In this sample app, I provide the code to alter a table by showing how to add a column and how to rename a table, both of which are part of the Alter Table statement. I am also providing the code to create a table, trigger, column, and view. Likewise, you will find code to drop (delete, remove) a table, view, trigger or index and even the database itself which, technically speaking, is not part of the SQLite SQL api. To manage the SQLite SQL query operations, I created a custom class as a sub-class of the NSObject. This custom class is called DatabaseOperations. I built the query selection process using blocks instead of the switch statement. The reason is because I wanted to be able to refer the selected queries by name instead by number (integer) which is the required parameter data type for the switch statement in C. Objective-C doesn’t have a switch statement. Before I get into the actual application and how it is built, I thought it might be a good idea to provide the queries of each of the SQLite operations to modify a database. These are provided in the next section. SQLite Queries I took the liberty to hardcode the names of the objects to be modified, preferring to focus on how to setup the query for each type of possible modification instead. In a production environment, the hardcoded values would be replaced with variables with the new value. All the queries use the NSString class to store the query string because I find it easier to manage concatenation than using the C based const char * variable name design. In the following code I am providing just the example queries, in the next section I will demonstrate how to use them. Updating an iOS SQLite Database 2 Alter Table 1 NSString*(^at)(void) = ^{ 2 NSString * alterTbl = [[NSString alloc]init]; 3 alterTbl = @"Alter Table todoTbl Rename to bar"; 4 return alterTbl; 5 }; Add Column 1 NSString *(^ac)(void)=^{ 2 NSString * addCol = [[NSString alloc]init]; 3 addCol = @"Alter Table bar Add column baz CHAR(50)"; 4 return addCol; 5 }; Create Table 1 NSString *(^ct)(void)=^{ 2 NSString *addTbl = [[NSString alloc]init]; 3 addTbl = @"create table if not exists todoTbl(todoName varchar, 4 todoDescription varchar, todoDate varchar)"; 5 return addTbl; 6 }; Create Index 1 NSString *(^ci)(void)= ^{ 2 NSString *addIn =[[NSString alloc]init]; 3 addIn = @"Create index if not exists fooindex on bar(todoName)"; 4 return addIn; 5 }; Create Trigger Updating an iOS SQLite Database 3 1 NSString*(^ctrg)(void)=^{ 2 NSString *addTrig =[[NSString alloc]init]; 3 addTrig = @"Create trigger if not exists baztrig after insert on bar begin i\ 4 nsert 5 into todoTblLog(todoName,todoDescription, Time) values('foo','bar', 6 datetime('now')); end"; 7 return addTrig; 8 }; Create View 1 NSString *(^cv)(void)=^{ 2 NSString *addVw =[[NSString alloc]init]; 3 addVw = @"Create view if not exists aView as Select todoName, todoDescriptio\ 4 n 5 from bar"; 6 return addVw; 7 }; Drop Database See the DeleteDatabase method section in this chapter. Drop Table 1 NSString*(^dt)(void)= ^{ 2 NSString *dropTbl = [[NSString alloc]init]; 3 4 dropTbl = @"drop if exist bar "; 5 return dropTbl; 6 }; Drop Index Updating an iOS SQLite Database 4 1 NSString*(^di)(void)=^{ 2 NSString *dropIndex = [[NSString alloc]init]; 3 4 dropIndex = @"drop if exists fooIndex"; 5 return dropIndex; 6 }; Drop Trigger 1 NSString*(^dtrg)(void)= ^{ 2 NSString *dropTrig = [[NSString alloc]init]; 3 dropTrig = @"drop if exists baztrig"; 4 return dropTrig; 5 }; Drop View 1 NSString*(^dv)(void) = ^{ 2 NSString *dropView = [[NSString alloc]init]; 3 dropView = @"drop if exists aView"; 4 return dropView; 5 }; Develop the SQLite iPhone Application Since the purpose of the application is to show you how to write the code to be able to modify a SQLite the UI has limited functionality and I focus primarily on the interaction between the SQLite engine, the queries and the controller. Let’s start by creating a new iOS Single View iPhone app. You can get that set up through Xcode list of app templates by either selecting that template from the new project dashboard or selecting New Project from the Xcode menu. Once the project is created, add the SQLite library through the Linked Libraries and Frameworks section in the Project Summary page. For newbies, select the project root in the project explorer and the right side of the IDE, or main window, this is the Project Summary page. Scroll to the bottom and you will find the Linked Library and Frameworks section. By clicking on the “+” button, a search panel will appear allowing you to enter the “sqlite3” search term. Select the sqlite3.dylib. Updating an iOS SQLite Database 5 Create the Model To handle the database operations and the interactions with the actually databases, I created a NSObject subclass called DatabaseOperations. This object provides the implementation of the SQLite queries. As you see from the code listing below on the header file, you need to import the SQLite3 library using the import statement followed by the name of the library in angle brackets. Since I will need a pop a warning message and I will need to implement the UIAlert. I need to the UIAlertViewDelegate protocol which provides the interactivity to the UIAlertView. Next I declare a sqlite3 variable which will be our main conduit to the SQLite database engine. I wanted to use constants to declare the various database operations, so I have declare these constants using the FOUNDATION_EXPORT which is part of the NSObjCRuntime class. Mind you, I could have used the extern directive in C. I will use these later in the implementation with a Block which is a closure function or an inline function if you prefer. If you want to be really well organized and will need constants in different parts of the app, you could place all these constants in constants.h file and implementation object. Most of the operations happen inside a SQLite database but we need a method to allow us to create databases. So the BuildDatabase method will take a database name string as input and use that value to create database that will be used for our modifications at runtime. The next method, ExecuteDatabaseOperation, will implement the Block code in order to select the proper query and launch the ModifyDatabase method. You will notice that these methods don’t have any facilities to receive or pass the values of the objects to the changed. I did this on purpose to keep the example simple. In a real app you would need to provide a parameter or parameters to allow a user to provide the values for the requested changes. The blocks are added to a NSDictionary object. The other methods include the DeleteDatabase will be use to delete the database file. The Modify- Database will be used to get the Sqlite database object, get the database path and execute the selected query from the ExecuteDatabaseOperation once the database is opened. I took the liberty of hard coding the database name instead of using the database para- meter. To populate the UIPickerView, I created the GetDatabaseList method that returns a list of SQLite database in the Documents path. The last method, WarningMessage will display the UIAlertView when needed. 1 // 2 // DatabaseOperations.h 3 // Update SQLite Database 4 // 5 // Created by Kevin Languedoc on 4/28/14. 6 // Copyright (c) 2014 Kevin Languedoc.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    29 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us