官术网_书友最值得收藏!

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)
主站蜘蛛池模板: 湘乡市| 开远市| 鄂尔多斯市| 新泰市| 清苑县| 汾西县| 察隅县| 三门峡市| 辉县市| 虹口区| 龙门县| 黄大仙区| 都兰县| 长汀县| 烟台市| 丽江市| 乌鲁木齐市| 绍兴县| 霍山县| 文山县| 万年县| 甘谷县| 伊春市| 奉贤区| 伊吾县| 盖州市| 乌兰察布市| 贵港市| 久治县| 新野县| 民和| 新泰市| 新龙县| 宜宾市| 达日县| 新源县| 元江| 西乌珠穆沁旗| 桂平市| 都江堰市| 山丹县|