GitHub地址 SQLite.swift
SQLite.swift使用Swift 编写的一款SQLite工具,封装了各种sql操作,避免了纯sql语句的开发
安装
- Carthage
github "stephencelis/SQLite.swift" ~> 0.12.0
- CocoaPods安装
pod 'SQLite.swift', '~> 0.12.0'
- Swift Package Manager
dependencies: [
.package(url: "https://github.com/stephencelis/SQLite.swift.git", from: "0.12.0")
]
使用
- 首先第一步创建连接SQLite的对象 这一步就相当于创建了数据库,但是数据库中并没有表
//sqlPath是你数据库文件的地址,如..../demo.sqlite
let db = try? Connection.init(sqlPath)
- 创建数据库的表
let id = Expression.init("uId")
let name = Expression.init("userName")
do{
let table = Table.init(tableName)
try db?.run(table.create(temporary: false, ifNotExists: true, withoutRowid: false, block: { (builder) in
//设置主键和自增
bulider.column(id,primaryKey: .autoincrement)
bulider.column(name)
}))
}catch(let error){
}
- 向数据库中增加数据
@discardableResult func insert (table:Table?,setters:[Setter]) -> Bool{
guard let tab = table else {
return false
}
do {
try db?.run(tab.insert(setters))
return true
} catch let error {
debugPrint(error.localizedDescription)
return false
}
}
///
- 查询数据
func select(table:Table?,select: [Expressible] = [],filter: Expression? = nil, order: [Expressible] = [], limit: Int? = nil, offset: Int? = nil) -> [Row]? {
guard var queryTable = table else {
return nil
}
do {
if select.count != 0{
queryTable = queryTable.select(select).order(order)
}else{
queryTable = queryTable.order(order)
}
if let filterTemp = filter {
queryTable = queryTable.filter(filterTemp)
}
if let lim = limit{
if let off = offset {
queryTable = queryTable.limit(lim, offset: off)
}else{
queryTable = queryTable.limit(lim)
}
}
guard let result = try db?.prepare(queryTable) else { return nil }
return Array.init(result)
} catch let error {
debugPrint(error.localizedDescription)
return nil
}
}
- 更新数据
@discardableResult func update(table:Table?,setters:[Setter],filter: Expression? = nil) -> Bool {
guard var filterTable = table else {
return false
}
do {
if let filterTemp = filter {
filterTable = filterTable.filter(filterTemp)
}
try db?.run(filterTable.update(setters))
return true
} catch let error {
debugPrint(error.localizedDescription)
return false
}
}
- 删除数据
@discardableResult func delete(table:Table?,filter: Expression? = nil) -> Bool{
guard var filterTable = table else {
return false
}
do {
if let filterTemp = filter {
filterTable = filterTable.filter(filterTemp)
}
try db?.run(filterTable.delete())
return true
} catch let error {
debugPrint(error.localizedDescription)
return false
}
}
综合上面写的工具
import Foundation
import SQLite
class SqLiteManger{
private var db:Connection?
init(sqlPath:String) {
db = try? Connection.init(sqlPath)
db?.busyTimeout = 5.0
}
}
struct TableColumn {
var cid:Int64?
var name:String?
var type:String?
var notnul:Int64?
var defaultValue:Any?
var primaryKey:Int64?
}
extension SqLiteManger{
func createTable(tableName:String, block: (TableBuilder) -> Void) -> Table? {
do{
let table = Table.init(tableName)
try db?.run(table.create(temporary: false, ifNotExists: true, withoutRowid: false, block: { (builder) in
block(builder)
}))
return table
}catch(let error){
debugPrint(error.localizedDescription)
return nil
}
}
@discardableResult func deleteTable(tableName:String) -> Bool {
let exeStr = "drop table if exists \(tableName) "
do {
try db?.execute(exeStr)
return true
}catch(let error){
debugPrint(error.localizedDescription)
return false
}
}
@discardableResult func updateTable(oldName:String,newName:String) -> Bool {
let exeStr = "alter table \(oldName) rename to \(newName) "
do {
try db?.execute(exeStr)
return true
}catch(let error){
debugPrint(error.localizedDescription)
return false
}
}
}
extension SqLiteManger{
@discardableResult func addColumn(tableName:String,columnName:String,columnType:String) -> Bool {
let exeStr = "alter table \(tableName) add \(columnName) \(columnType) "
do {
try db?.execute(exeStr)
return true
}catch(let error){
debugPrint(error.localizedDescription)
return false
}
}
func checkColumnExist(tableName:String,columnName:String) -> Bool {
return allColumns(tableName: tableName).filter { (model) -> Bool in
return model.name == columnName
}.count != 0
}
func allColumns(tableName:String) -> [TableColumn] {
let exeStr = "PRAGMA table_info([\(tableName)]) "
do {
let stmt = try db?.prepare(exeStr)
guard let result = stmt else {
return []
}
var columns:[TableColumn] = []
for case let row in result {
guard row.count == 6 else {
continue
}
let column = TableColumn.init(cid: row[0] as? Int64, name: row[1] as? String, type: row[2] as? String, notnul: row[3] as? Int64 ?? 0, defaultValue: row[4], primaryKey: row[5] as? Int64 ?? 0)
columns.append(column)
print(row)
}
return columns
}catch(let error){
debugPrint(error.localizedDescription)
return []
}
}
}
extension SqLiteManger{
@discardableResult func insert (table:Table?,setters:[Setter]) -> Bool{
guard let tab = table else {
return false
}
do {
try db?.run(tab.insert(setters))
return true
} catch let error {
debugPrint(error.localizedDescription)
return false
}
}
@discardableResult func delete(table:Table?,filter: Expression? = nil) -> Bool{
guard var filterTable = table else {
return false
}
do {
if let filterTemp = filter {
filterTable = filterTable.filter(filterTemp)
}
try db?.run(filterTable.delete())
return true
} catch let error {
debugPrint(error.localizedDescription)
return false
}
}
@discardableResult func update(table:Table?,setters:[Setter],filter: Expression? = nil) -> Bool {
guard var filterTable = table else {
return false
}
do {
if let filterTemp = filter {
filterTable = filterTable.filter(filterTemp)
}
try db?.run(filterTable.update(setters))
return true
} catch let error {
debugPrint(error.localizedDescription)
return false
}
}
func select(table:Table?,select: [Expressible] = [],filter: Expression? = nil, order: [Expressible] = [], limit: Int? = nil, offset: Int? = nil) -> [Row]? {
guard var queryTable = table else {
return nil
}
do {
if select.count != 0{
queryTable = queryTable.select(select).order(order)
}else{
queryTable = queryTable.order(order)
}
if let filterTemp = filter {
queryTable = queryTable.filter(filterTemp)
}
if let lim = limit{
if let off = offset {
queryTable = queryTable.limit(lim, offset: off)
}else{
queryTable = queryTable.limit(lim)
}
}
guard let result = try db?.prepare(queryTable) else { return nil }
return Array.init(result)
} catch let error {
debugPrint(error.localizedDescription)
return nil
}
}
}
Demo地址