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)
? ? ? ? }
? ? ? ? //導(dǎo)入數(shù)據(jù) 可用for循環(huán)批量導(dǎo)入
? ? ? ? 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? {
? ? ? ? }
? ? ? ? //查詢 —— 所有數(shù)據(jù)
? ? ? ? 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? {
? ? ? ? }
? ? ? ? //修改一個(gè)參數(shù)
? ? ? ? let updateItem =Table("messageList").filter(Expression<Int64>("noticeId") ==3)
? ? ? ? do{
? ? ? ? ? ? if try db.run(updateItem.update(Expression<Int64>("state") <-10))>0{
? ? ? ? ? ? }else{
? ? ? ? ? ? }
? ? ? ? }catch{
? ? ? ? }
? ? ? ? //修改多個(gè)參數(shù)
? ? ? ? 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? {
? ? ? ? }
? ? }
}