第三方庫的導(dǎo)入方法這里不講解。
新建一個(gè)iOS程序,并新建一個(gè)swift文件命名為Database并建立結(jié)構(gòu)體。
//
// Database.swift
// iOS_Client
//
// Created by AlexTiger on 2020/13/33.
// Copyright ? 2020年 Tiger. All rights reserved.
//
import Foundation
import SQLite
struct Database {
var db: Connection!
init() {
connectDatabase()
}
mutating func connectDatabase(filePath: String = "/Documents") -> Void {
let sqlFilePath = NSHomeDirectory() + filePath + "/db.sqlite3"
do {
// 與數(shù)據(jù)庫建立連接
db = try Connection(sqlFilePath)
print("與數(shù)據(jù)庫建立連接 成功")
} catch {
print("與數(shù)據(jù)庫建立連接 失敗:\(error)")
}
}
// ============== 燈光 ===============
let TABLE_LAMP = Table("table_lamp") // 表名稱
let TABLE_LAMP_ID = Expression<Int64>("lamp_id") // 列表項(xiàng)及項(xiàng)類型
let TABLE_LAMP_ADDRESS = Expression<Int64>("lamp_address")
let TABLE_LAMP_NAME = Expression<String>("lamp_name")
let TABLE_LAMP_COLOR_VALUE = Expression<String>("lamp_colorValue")
let TABLE_LAMP_LAMP_TYPE = Expression<Int64>("lamp_lampType")
// 建表
func tableLampCreate() -> Void {
do {
// 創(chuàng)建表TABLE_LAMP
try db.run(TABLE_LAMP.create { table in
table.column(TABLE_LAMP_ID, primaryKey: .autoincrement) // 主鍵自加且不為空
table.column(TABLE_LAMP_ADDRESS)
table.column(TABLE_LAMP_NAME)
table.column(TABLE_LAMP_COLOR_VALUE)
table.column(TABLE_LAMP_LAMP_TYPE)
})
print("創(chuàng)建表 TABLE_LAMP 成功")
} catch {
print("創(chuàng)建表 TABLE_LAMP 失斔柘肌:\(error)")
}
}
// 插入
func tableLampInsertItem(address: Int64, name: String, colorValue: String, lampType: Int64) -> Void {
let insert = TABLE_LAMP.insert(TABLE_LAMP_ADDRESS <- address, TABLE_LAMP_NAME <- name, TABLE_LAMP_COLOR_VALUE <- colorValue, TABLE_LAMP_LAMP_TYPE <- lampType)
do {
let rowid = try db.run(insert)
print("插入數(shù)據(jù)成功 id: \(rowid)")
} catch {
print("插入數(shù)據(jù)失敗: \(error)")
}
}
// 遍歷
func queryTableLamp() -> Void {
for item in (try! db.prepare(TABLE_LAMP)) {
print("燈光 遍歷 ———— id: \(item[TABLE_LAMP_ID]), address: \(item[TABLE_LAMP_ADDRESS]), name: \(item[TABLE_LAMP_NAME]), colorValue: \(item[TABLE_LAMP_COLOR_VALUE]), lampType: \(item[TABLE_LAMP_LAMP_TYPE])")
}
}
// 讀取
func readTableLampItem(address: Int64) -> Void {
for item in try! db.prepare(TABLE_LAMP.filter(TABLE_LAMP_ADDRESS == address)) {
print("\n讀取(燈光)id: \(item[TABLE_LAMP_ID]), address: \(item[TABLE_LAMP_ADDRESS]), name: \(item[TABLE_LAMP_NAME]), colorValue: \(item[TABLE_LAMP_COLOR_VALUE]), lampType: \(item[TABLE_LAMP_LAMP_TYPE])")
}
}
// 更新
func tableLampUpdateItem(address: Int64, newName: String) -> Void {
let item = TABLE_LAMP.filter(TABLE_LAMP_ADDRESS == address)
do {
if try db.run(item.update(TABLE_LAMP_NAME <- newName)) > 0 {
print("燈光\(address) 更新成功")
} else {
print("沒有發(fā)現(xiàn) 燈光條目 \(address)")
}
} catch {
print("燈光\(address) 更新失斊杞洹:\(error)")
}
}
// 刪除
func tableLampDeleteItem(address: Int64) -> Void {
let item = TABLE_LAMP.filter(TABLE_LAMP_ADDRESS == address)
do {
if try db.run(item.delete()) > 0 {
print("燈光\(address) 刪除成功")
} else {
print("沒有發(fā)現(xiàn) 燈光條目 \(address)") }
} catch {
print("燈光\(address) 刪除失敺娇狻:\(error)")
}
}
}
下面在ViewController.swift中調(diào)用以上方法
class ViewController: UIViewController {
// 數(shù)據(jù)庫聲明
var database: Database!
override func viewDidLoad() {
super.viewDidLoad()
// 與數(shù)據(jù)庫建立連接
database = Database()
// 建立列表(有列表后不再建立)
database.tableLampCreate()
// 插入兩條數(shù)據(jù)
database.tableLampInsertItem(address: 51, name: "燈光1", colorValue: "#FFFFFF", lampType: 0)
database.tableLampInsertItem(address: 52, name: "燈光2", colorValue: "#AAAAAA", lampType: 1)
// 遍歷列表(檢查插入結(jié)果)
database.queryTableLamp()
// 根據(jù)條件查詢
database.readTableLampItem(address: 52)
// 修改列表項(xiàng)
database.tableLampUpdateItem(address: 51, newName: "客廳大燈")
// 遍歷列表(檢查修改結(jié)果)
database.queryTableLamp()
// 刪除列表項(xiàng)
database.tableLampDeleteItem(address: 52)
// 遍歷列表(檢查刪除結(jié)果)
database.queryTableLamp()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
顯示結(jié)果
與數(shù)據(jù)庫建立連接 成功
2019-04-16 20:49:10.802019+0800 InvoiceWallet[30026:2016677] [logging] table "table_lamp" already exists
創(chuàng)建表 TABLE_LAMP 失敗:table "table_lamp" already exists (code: 1)
插入數(shù)據(jù)成功 id: 3
插入數(shù)據(jù)成功 id: 4
燈光 遍歷 ———— id: 1, address: 51, name: 客廳大燈, colorValue: #FFFFFF, lampType: 0
燈光 遍歷 ———— id: 3, address: 51, name: 燈光1, colorValue: #FFFFFF, lampType: 0
燈光 遍歷 ———— id: 4, address: 52, name: 燈光2, colorValue: #AAAAAA, lampType: 1
讀日险(燈光)id: 4, address: 52, name: 燈光2, colorValue: #AAAAAA, lampType: 1
燈光51 更新成功
燈光 遍歷 ———— id: 1, address: 51, name: 客廳大燈, colorValue: #FFFFFF, lampType: 0
燈光 遍歷 ———— id: 3, address: 51, name: 客廳大燈, colorValue: #FFFFFF, lampType: 0
燈光 遍歷 ———— id: 4, address: 52, name: 燈光2, colorValue: #AAAAAA, lampType: 1
燈光52 刪除成功
燈光 遍歷 ———— id: 1, address: 51, name: 客廳大燈, colorValue: #FFFFFF, lampType: 0
燈光 遍歷 ———— id: 3, address: 51, name: 客廳大燈, colorValue: #FFFFFF, lampType: 0