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
builder.column(Expression
builder.column(Expression
builder.column(Expression
builder.column(Expression
builder.column(Expression
builder.column(Expression
builder.column(Expression
builder.column(Expression
builder.column(Expression
builder.column(Expression
}
do{
try db.run(create)
}catch {
print(error.localizedDescription)
}
//导入数据 可用for循环批量导入
let insert = messageList.insert(Expression
do{
try db.run(insert)
}catch {
}
//查询 —— 所有数据
var array = [Dictionary
do{
//条件查询
//for item in try db.prepare(Table("messageList").filter(Expression("ownerId") == 1)) {
for item in try db.prepare(Table("messageList")) {
var dic =Dictionary
dic.updateValue(item[Expression
dic.updateValue(item[Expression
dic.updateValue(item[Expression
dic.updateValue(item[Expression
dic.updateValue(item[Expression
dic.updateValue(item[Expression
dic.updateValue(item[Expression
dic.updateValue(item[Expression
dic.updateValue(item[Expression
dic.updateValue(item[Expression
array.append(dic)
}
}catch {
}
//修改一个参数
let updateItem =Table("messageList").filter(Expression
do{
if try db.run(updateItem.update(Expression
}else{
}
}catch{
}
//修改多个参数
let updateMoreItem =Table("messageList").filter(Expression
do{
iftrydb.run(updateMoreItem.update([Expression
}else{
}
}catch{
}
//删除
let deleteItem =Table("messageList").filter(Expression
//删除全部
//let item = Table("messageList")
do{
if try db.run(deleteItem.delete()) >0{
}else{
}
}catch {
}
}
}