模型代码:模型中包含模型数组
import UIKit
class DispatchInfoModel:NSObject{
var cfpInfo:String!
var flightId:String!
var teleInfo:String!
var weatherPic:NSMutableArray!
override static func mj_objectClassInArray() -> [AnyHashable:Any]! {
return["weatherPic":WeatherPicModel.self]
}
}
//存储自定义的对象关键就是要转换成NSData类型并已BLOB类型存储。此处需要用到归档和解档
class WeatherPicModel:NSObject,NSCoding{
var flightId:String!
var picMemo:String!
// MARK:-处理需要归档的字段
func encode(with aCoder:NSCoder) {
aCoder.encode(self.flightId, forKey:"flightId")
aCoder.encode(self.picMemo, forKey:"picMemo")
}
// MARK:-处理需要解档的字段
required init?(coder aDecoder:NSCoder) {
super.init()
self.flightId= aDecoder.decodeObject(forKey:"flightId")as!String!
self.picMemo= aDecoder.decodeObject(forKey:"picMemo")as!String!
}
override init(){
super.init()
}
}
数据库代码:
import UIKit
import FMDB
class DispatchInfoModelManger:NSObject{
static let defaultManger =DispatchInfoModelManger()
//创建和打开一个数据库
//如果有就直接打开,如果没有,创建一个再打开
lazy var fmdb:FMDatabase= {
let path =NSHomeDirectory().appending("/Documents/dispatchInfo.db")
let db =FMDatabase(path: path)
return db!
}()
//实例化db对象的时候就会默认打开或创建一个数据库
override init(){
super.init()
if fmdb.open(){
print(NSHomeDirectory().appending("/Documents/dispatchInfo.db"))
print("打开数据库成功")
}
}
//建表
func creatTable()->Void{
let creatSql ="create table if not exists dispatchInfo(id integer primary key autoincrement,cfpInfo var char(256),flightId varchar(256),teleInfo varchar(256),weatherPic BLOB)"
let result =fmdb.executeUpdate(creatSql, withArgumentsIn:nil)
if result{
print("创建表成功")
}
}
//删除表
func dropTable()->Void{
let sql ="drop table if exists dispatchInfo"
let result =fmdb.executeUpdate(sql, withArgumentsIn:nil)
if result{
print("删除表成功")
}
}
//插入数据
func insert(model:DispatchInfoModel)->Void{
//数组weatherPic转化为Data类型
let weatherPicData =NSKeyedArchiver.archivedData(withRootObject: model.weatherPic)
let insertSql ="insert into dispatchInfo(cfpInfo,flightId,teleInfo,weatherPic) values(?,?,?,?)"
do{
try fmdb.executeUpdate(insertSql, values: [model.cfpInfo,model.flightId,model.teleInfo,weatherPicData])
}catch{
print(fmdb.lastError())
}
}
//更新数据
func updateWith(model:DispatchInfoModel,uid:Int)->Void{
//数组weatherPic转化为Data类型
let weatherPicData =NSKeyedArchiver.archivedData(withRootObject: model.weatherPic)
let updateSql ="update dispatchInfo set cfpInfo = ?,flightId = ?,teleInfo = ?,weatherPic = ? where id = ?"
do{
try fmdb.executeUpdate(updateSql, values: [model.cfpInfo,model.flightId,model.teleInfo,weatherPicData,uid])
}catch{
print(fmdb.lastError())
}
}
//查询全部数据
func selectAll()->[DispatchInfoModel]{
var tmpArr = [DispatchInfoModel]()
let selectSql ="select * from dispatchInfo"
do{
let rs =tryfmdb.executeQuery(selectSql, values:nil)
while rs.next() {
let model =DispatchInfoModel()
model.cfpInfo= rs.string(forColumn:"cfpInfo")
model.flightId= rs.string(forColumn:"flightId")
model.teleInfo= rs.string(forColumn:"teleInfo")
let weatherPicData = rs.data(forColumn:"weatherPic")
model.weatherPic=NSKeyedUnarchiver.unarchiveObject(with: weatherPicData!)as!NSMutableArray!
tmpArr.append(model)
}
}catch{
print(fmdb.lastError())
}
returntmpArr
}
}
测试本地数据:
func testDispatchInfoModel(){
let path =Bundle.main.path(forResource:"放行资料", ofType:"json")
do{
let apiData:Data=tryData.init(contentsOf:URL.init(fileURLWithPath: path!))
let json =tryJSONSerialization.jsonObject(with: apiData, options:JSONSerialization.ReadingOptions.mutableContainers)
let jsonDic = jsonas!Dictionary
let datalist = jsonDic["list"]as!NSDictionary
let dispatch =DispatchInfoModel.mj_object(withKeyValues: datalist)asDispatchInfoModel
DispatchInfoModelManger.defaultManger.creatTable()
DispatchInfoModelManger.defaultManger.insert(model: dispatch)
print(dispatch)
let model =DispatchInfoModelManger.defaultManger.selectAll()
print(model[0].cfpInfo)
print((model[0].weatherPic[0]as!WeatherPicModel).picMemo)
}
catch{
print(error)
}
}
网友评论