Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning SQLite for iOS

You're reading from  Learning SQLite for iOS

Product type Book
Published in Mar 2016
Publisher
ISBN-13 9781785288975
Pages 154 pages
Edition 1st Edition
Languages
Author (1):
Gene Da Rocha Gene Da Rocha
Profile icon Gene Da Rocha
Toc

Table of Contents (15) Chapters close

Learning SQLite for iOS
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
1. Introduction to SQL and SQLite 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:

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:

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 following is a screenshot showing the SimpleCalculator application opened in Xcode. Select the iPhone 6s option as the device to develop on:

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 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:

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 ₹800/month. Cancel anytime}