Using the SQLite 3 Library

Before starting to code, you must add a specific library for SQLite. Within Xcode, ensure that the General tab is selected and visible. At the end of the page, look out for the Linked Frameworks and Libraries section. View and click on the icon with the plus sign, as shown in the following screenshot:

Using the SQLite 3 Library

A modal window will appear; please enter the word sqlite, and from the list of information, select the libsqlite3.dylib option, as shown in the following screenshot:

Using the SQLite 3 Library

After selecting it, click on the Add button to move forward.

As part of this chapter, we will use FMDB and an Objective-C wrapper around SQLite to show the database SQLite with Swift. FMDB stands for Flying Meat Database, and it easily interacts with SQLite and actually saves time and effort. For example, the FMDB will be linked to one SQLite database and will be there for the execution of SQL queries. The output is FMResultsSet, which shows results for queries executed on the FMDB.

The FMDB approach with an application that performs the SQL commands of Insert, Select, Delete, and Update will be used on a table called Mortgage. The Mortgage table will have a Name field and a Mortgage Roll Number field for the account details, as a very simple example.

The following is a screenshot of the actual user design using the View controller scene tool within Xcode to create the basic screen layout. This will outline and show the resulting action to be followed and executed when a button is clicked:

Using the SQLite 3 Library

There are many tools to manage the SQLite database, and one of these is the SQLite Manager Add-on in the Firefox browser, to administer the database. The add-on can be obtained from https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/.

The add-on product has some of the following features for administrators:

Open Firefox and install the extensions from the aforementioned link. The following screenshot shows how the extension is installed:

Using the SQLite 3 Library

When you click on the Install Now button, the add-on is installed. Then go to Tools | Menu Option and you will find SQLite Manager. Once you click on that option, the following screen will appear:

Using the SQLite 3 Library

In order to add data to the system, the database must be created.

When you click on the Install Now button, the add-on is installed. Once you go to Tools | Menu Option, you will see the SQLite Manager option. When you click on that option, the following screen is seen.

Create the new database and ensure that a table is created for this small test application with SQLite. In this scenario, we have a database called Mortgagedata.sqlite, and a table called Mortgage_data with two fields: mortgage_rollno and mortgage_name.

The following screenshot shows the screens to call up SQL Manager in Firefox:

Using the SQLite 3 Library

When you click on the SQL Manager option to create a database, a pop up will be displayed where the user enters the database name, as shown in the following screenshot:

Using the SQLite 3 Library

Next, after the database is created, the user has to select a disk directory to store the database in, as shown in the next screenshot:

Using the SQLite 3 Library

Next, you need to create a table, as shown in the preceding screenshot, with two rows:

Using the SQLite 3 Library

The preceding screenshot now shows what the table structure looks like before further work is carried out.

Next, a class, that is a subclass of NSObject, has to be created. Call this class Mortgage_data and set up its properties as per the requirements of this test scenario. These properties will form part of the database schema and foundations for your small database and table:

Class Mortgage_data: NSObject {
    Varmortgage_rollno: String = String()
    Varmortgage_name: String = String()
}

FMDB is a SQLite library written on top of SQLite to handle database operations easier.

Since FMDB is built in Objective-C, a bridging header is required to link it up with the simple app. A .h file has to be included, which is FMDatabase.h.

This is required to ensure that the linking works for the Objective-C and Swift technology produce the results we want. Next, we need to create a database in SQLite Manager and copy it to the right place.

In the following code, we show the function to copy a database. In this instance, we need to create and use a function called copyFile, which will copy the files to the application's document directory. Methods such as applicationDidFinishLaunch work with AppDelegate by passing the database name in, as part of the requirements argument.

See the following class:

When using the FMDB way of interacting with SQLite, a class called ModelManager is created as part of NSObject{}, so that a variety of functions can reside, or be inserted, within it. This is mandatory and is shown in the next code snippet.

In the following code, you will see that after the database object has been copied, it needs to be initialized, a Swift file has to be named and added to the ModelManager, and a shared instance of the type of ModelManager has to be defined out of the class block, as shown in the following:

Then, set up the database object for the FMDB, namely, the FMDatabase object, and together with the database object, set up and reset it as shown in the following:

The next part of the process is to interrogate the database with the following command, the insert command, as an example wrapped in the assigned value Mortage_Inserted. The other class, like ModelManager, will have the Mobile_data method added to it. The ModelManager method is then trying to open the Mortgage database by using the method for opening, namely, the FMDatabase class. Then the executeUpdate method is allocated and used as part of the FMDatabase class to push and pass down the SQL query and the input parameters. To finish this off, part of the operation, the close method, is used. See the following code on how the database is connected:

The Insert Button has an action method; call this method to send through the Mortage_data class, which holds the mortgage_rollno and mortgage_name field names, as shown in the following:

FMDB is well documented and popular on the Internet. Next, the actual SQL query is passed through the executeUpdate method, which is part of the FMDatabase class and is linked up as parameters, as part of an argument:

The following code shows how the @IBAction function is called using the btnUpdateClicked function with the relevant fields and text information, to perform the update based on the click of the button called btnUpdateClicked:

To perform the delete operation, use the DeleteMortgateData method. First, the ModelManager class is used to open the database, using the FMDatabase class as utilized previously. Thereafter, again use the executeUpdate method, and using an argument, pass the SQL delete query and close the database by invoking the close method, as defined in the FMDatabase class. Details of this operation are as follows:

As previously used, the same ModelManager method is used for SQL operations:

The next operation is the SelectMortgageData operation, which will be added to the ModelManager method. This will open the database by using the open method within the FMDatabase class. This is then followed by the executeQuery method, using the FMDatabase class, which accepts the input SQL query:

The following method is called from the previous action:

The SQLite scheme using Firefox can be viewed as shown in the following screenshot. Go to Firefox, and under the Tools menu, invoke SQLite Manager. Click on the Mortgage Manager table and you can see the structure of the table, as shown in the following screenshot:

Using FMDB

An alternate method for using FMDB this way is to use it directly in the Swift programming language, as shown in the following code. When setting up Xcode with Swift, you will be prompted for the bridging component for linking them up, but it can be done as described here.

The code is added in the place where the IBAction is set up. The method will require the use of the SaveMortgageData action method, which will open the database, take text/information from the data fields, build an SQL database, and execute the statement. Once the operation is complete, the database will be closed. Then the text fields will be initialized, ready for the next input. Therefore, the IBAction method using the default template will have to be modified as follows:

After using this creation process and method for an application that is created with Swift and SQLite, you can see how the basic components and the SQLite 3 library have been successfully added to the new project.