简单的Sqlite使用方法
Sqlite的作用不言而喻,就是帮助开发者建立本地数据库,存储一些基本信息。不做过多阐述,需要的深入了解的可以找度娘,本文是针对Sqlite小白在使用上提供一些实例。
一、导入Sqlite.swift
pod 'SQLite.swift'
二、建立数据库实例
以下表字段做实例说明。
(1)与数据库建立连接
//与数据库建立连接 mutating func connectDataBase(filePath:String = "/Documents") -> Void { let sqliteFilePath = NSHomeDirectory() + filePath + "/db.sqlite3" do { //与数据库建立连接 db = try Connection(sqliteFilePath) print("与数据库建立连接 成功") }catch{ print("与数据库建立连接 失败:\(error)") } }
(2)建立TATLE_LAMP表
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)") } }
(3)插入记录
//插入 func tableLampInsert(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)") } }
(4)遍历数据库及读取某条记录
//遍历数据库 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 tableLampRead(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])") } }
(5)更新单条记录
//更新 func tableLampUpdate(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)") } }
(6)删除单条记录
//删除 func tableLampDelete(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)") } }
(7)使用数据库完成常规操作
//数据库建立连接 dataBase = NADataBaseSqlite() //建立列表(有列表后不再建立) dataBase.tableLampCreate() //插入两条数据 dataBase.tableLampInsert(address: 51, name: "灯光1", colorValue: "#FFFFFF", lampType: 0) dataBase.tableLampInsert(address: 52, name: "灯光2", colorValue: "#AAAAAA", lampType: 1) //遍历列表(检查插入结果) dataBase.queryTableLamp() //根据条件查询 dataBase.tableLampRead(address: 52) //修改列表项 dataBase.tableLampUpdate(address: 51, newName: "客厅大灯") //遍历列表(检查修改结果) dataBase.queryTableLamp() //删除列表项 dataBase.tableLampDelete(address: 52) //遍历列表(检查删除结果) dataBase.queryTableLamp()
附:完成的实现代码
// // ViewController.swift // SqliteSwiftDemo // // Created by xjf on 2019/4/14. // Copyright © 2019 xfj. All rights reserved. // import UIKit import HandyJSON class ViewController: UIViewController { var dataBase : NADataBaseSqlite! override func viewDidLoad() { super.viewDidLoad() //数据库建立连接 dataBase = NADataBaseSqlite() //建立列表(有列表后不再建立) dataBase.tableLampCreate() //插入两条数据 dataBase.tableLampInsert(address: 51, name: "灯光1", colorValue: "#FFFFFF", lampType: 0) dataBase.tableLampInsert(address: 52, name: "灯光2", colorValue: "#AAAAAA", lampType: 1) //遍历列表(检查插入结果) dataBase.queryTableLamp() //根据条件查询 dataBase.tableLampRead(address: 52) //修改列表项 dataBase.tableLampUpdate(address: 51, newName: "客厅大灯") //遍历列表(检查修改结果) dataBase.queryTableLamp() //删除列表项 dataBase.tableLampDelete(address: 52) //遍历列表(检查删除结果) dataBase.queryTableLamp() } }
// // NADataBaseSqlite.swift // SqliteSwiftDemo // // Created by xjf on 2019/4/15. // Copyright © 2019 xjf. All rights reserved. // import Foundation import SQLite struct NADataBaseSqlite { var db : Connection! init() { connectDataBase() } //与数据库建立连接 mutating func connectDataBase(filePath:String = "/Documents") -> Void { let sqliteFilePath = NSHomeDirectory() + filePath + "/db.sqlite3" do { //与数据库建立连接 db = try Connection(sqliteFilePath) 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 tableLampInsert(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 tableLampRead(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 tableLampUpdate(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 tableLampDelete(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)") } } }
参考连接:Sqlite的增删改查