Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning SQLite for iOS

You're reading from   Learning SQLite for iOS Extend SQLite with mobile development skills to build great apps for iOS devices

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher
ISBN-13 9781785288975
Length 154 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Gene Da Rocha Gene Da Rocha
Author Profile Icon Gene Da Rocha
Gene Da Rocha
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Introduction to SQL and SQLite FREE CHAPTER 2. Database Design Concepts 3. Administering the Database 4. Essentials of SQL 5. Exposing the C API 6. Using Swift with iOS and SQLite 7. iOS Development with PhoneGap and HTML5 8. More Features and Advances in SQLite Index

The examples of using SQLite with iOS

The following is a simple application on how to use iOS with the SQLite database with Xcode. It outlines the basic steps of creating an application and database, and selecting data.

To get started, let's start Xcode and create a template using the Single View Application choice, as shown in the following screenshot:

The examples of using SQLite with iOS

Click on the Next button to proceed to the next screen in this process. In the product name field, enter SimpleCalculator for the language, and select Swift. For the devices field, select iPhone. Then, click on the Next button to move onto the next screen, as shown in the following screenshot:

The examples of using SQLite with iOS

In the following screenshot, select the directory where the code will reside. Now, we can view what the Xcode developer tool has created. Then, select a device to display the information; in our case, use the iPhone 6s.

See the directory for the source code, as shown in the following screenshot:

The examples of using SQLite with iOS

The following is a screenshot showing the SimpleCalculator application opened in Xcode. Select the iPhone 6s option as the device to develop on:

The examples of using SQLite with iOS

Next, click on the Play button that will compile and build the application as shown here in both images, and a blank screen will appear:

The examples of using SQLite with iOS

The preceding screenshot shows the application to be built, and the following screenshot shows a blank screen after the image is compiled and run.

This method gets you to the basics of an iOS application with Swift working as a canvas.

In this brief example, we will use SQLiteDB.swift and String-Extras.swift to work with the SQLite database, including the Bridging-Header.h file. In the Build Settings option, view Objective-C Bridging Header and double-click on it, and bridge it to Bridging-Header.h, and you can also drag it to show that it is linked.

As mentioned previously, add libsqlite3.0.dylib to the linked frameworks by navigating to General | Linked Frameworks and Libraries; then, add Libsqlite3.0.dylib.

Now, rebuild the project to show that it's working:

The examples of using SQLite with iOS

Click on the Simulator button, and then click on Quit to stop the current compiled simulator program. The program will compile with no problems. Next, a database instance has to be created as shown in the following code. The SQLite.DB.query method is used to execute these commands:

  • First an instance is required:
    let testdb = SQLiteDB.sharedInstance()
  • To run this query, the following code is used with the SQLiteDB.query way:
    var theresult = testdb.query("select * from people where county = 'Berks'", parameters: nil)
    for row in result
    {
        println(row["name"]!.asString())
    }
  • To delete a record for example, follow the following piece of code:
    testdb.execute("delete from people where county = 'Bucks' ", parameters: nil)
You have been reading a chapter from
Learning SQLite for iOS
Published in: Mar 2016
Publisher:
ISBN-13: 9781785288975
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime