第三方库的导入方法这里不讲解。
新建一个iOS程序,并新建一个swift文件命名为Database并建立结构体。
//
// 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 {
// 与数据库建立连接
db = try Connection(sqlFilePath)
print("与数据库建立连接 成功")
} catch {
print("与数据库建立连接 失败:\(error)")
}
}
// ============== 灯光 ===============
let TABLE_LAMP = Table("table_lamp") // 表名称
let TABLE_LAMP_ID = Expression<Int64>("lamp_id") // 列表项及项类型
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 {
// 创建表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("创建表 TABLE_LAMP 成功")
} catch {
print("创建表 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("插入数据成功 id: \(rowid)")
} catch {
print("插入数据失败: \(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("没有发现 灯光条目 \(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("没有发现 灯光条目 \(address)") }
} catch {
print("灯光\(address) 删除失败:\(error)")
}
}
}
下面在ViewController.swift中调用以上方法
class ViewController: UIViewController {
// 数据库声明
var database: Database!
override func viewDidLoad() {
super.viewDidLoad()
// 与数据库建立连接
database = Database()
// 建立列表(有列表后不再建立)
database.tableLampCreate()
// 插入两条数据
database.tableLampInsertItem(address: 51, name: "灯光1", colorValue: "#FFFFFF", lampType: 0)
database.tableLampInsertItem(address: 52, name: "灯光2", colorValue: "#AAAAAA", lampType: 1)
// 遍历列表(检查插入结果)
database.queryTableLamp()
// 根据条件查询
database.readTableLampItem(address: 52)
// 修改列表项
database.tableLampUpdateItem(address: 51, newName: "客厅大灯")
// 遍历列表(检查修改结果)
database.queryTableLamp()
// 删除列表项
database.tableLampDeleteItem(address: 52)
// 遍历列表(检查删除结果)
database.queryTableLamp()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
显示结果
与数据库建立连接 成功
2019-04-16 20:49:10.802019+0800 InvoiceWallet[30026:2016677] [logging] table "table_lamp" already exists
创建表 TABLE_LAMP 失败:table "table_lamp" already exists (code: 1)
插入数据成功 id: 3
插入数据成功 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
网友评论