SQLite.swift使用简单介绍
import UIKit
import SQLite
let ducumentPath = NSHomeDirectory() + "/Documents" + "/MMDB.sqlite"
class MMSQLite:NSObject{
static let shared:MMSQLite=MMSQLite()
var db: Connection!
private override init() {
db = try! Connection.init(ducumentPath)
}
func operate() {
//建表 不加?为NOTNULL,primaryKey:主键 unique:是否唯一
let messageList =Table("messageList")
let create = messageList.create{ (builder) in
builder.column(Expression<Int64>("uid"),unique:false)
builder.column(Expression<String?>("company_name"))
builder.column(Expression<String?>("name"))
builder.column(Expression<Int64>("id"))
builder.column(Expression<String?>("avatar"))
builder.column(Expression<String?>("job_title"))
builder.column(Expression<String?>("noticeId"),primaryKey:true)
builder.column(Expression<String?>("content"))
builder.column(Expression<String?>("create_time"))
builder.column(Expression<Int64>("state"))
builder.column(Expression<Int64>("ownerId"))
}
do{
try db.run(create)
}catch {
print(error.localizedDescription)
}
//导入数据 可用for循环批量导入
let insert = messageList.insert(Expression<Int64>("uid") <- 1,Expression<String>("company_name") <- "company_name",Expression<String>("name") <- "name",Expression<Int64>("id") <- 2,Expression<String>("avatar") <- "avatar",Expression<Int64>("noticeId") <- 3,Expression<Int64>("state") <- 4,Expression<String>("job_title") <- "job_title",Expression<String>("content") <- "content",Expression<String>("create_time") <- "create_time",Expression<Int64>("ownerId") <- 5)
do{
try db.run(insert)
}catch {
}
//查询 —— 所有数据
var array = [Dictionary<String, Any>].init()
do{
//条件查询
//for item in try db.prepare(Table("messageList").filter(Expression("ownerId") == 1)) {
for item in try db.prepare(Table("messageList")) {
var dic =Dictionary<String, Any>.init()
dic.updateValue(item[Expression<Int64>("id")] as NSNumber, forKey:"id")
dic.updateValue(item[Expression<Int64>("uid")] as NSNumber, forKey:"uid")
dic.updateValue(item[Expression<Int64>("state")] as NSNumber, forKey:"state")
dic.updateValue(item[Expression<Int64>("noticeId")] as NSNumber, forKey:"noticeId")
dic.updateValue(item[Expression<String>("name")] as String, forKey:"name")
dic.updateValue(item[Expression<String>("company_name")] as String, forKey:"company_name")
dic.updateValue(item[Expression<String>("avatar")] as String, forKey:"avatar")
dic.updateValue(item[Expression<String>("job_title")] as String, forKey:"job_title")
dic.updateValue(item[Expression<String>("content")] as String, forKey:"content")
dic.updateValue(item[Expression<String>("create_time")] as String, forKey:"create_time")
array.append(dic)
}
}catch {
}
//修改一个参数
let updateItem =Table("messageList").filter(Expression<Int64>("noticeId") ==3)
do{
if try db.run(updateItem.update(Expression<Int64>("state") <-10))>0{
}else{
}
}catch{
}
//修改多个参数
let updateMoreItem =Table("messageList").filter(Expression<Int64>("noticeId") ==3)
do{
iftrydb.run(updateMoreItem.update([Expression<Int64>("state") <-10,Expression<String>("name") <- "newName"]))>0{
}else{
}
}catch{
}
//删除
let deleteItem =Table("messageList").filter(Expression<Int64>("noticeId") ==3)
//删除全部
//let item = Table("messageList")
do{
if try db.run(deleteItem.delete()) >0{
}else{
}
}catch {
}
}
}