直接上代码:
import UIKit class ZWDBManager: NSObject { //前提将FMDBDatabase的头文件加入到桥接文件中 var dataBase:FMDatabase? var lock:NSLock? //创建单例 class func shareInstance()->ZWDBManager{ struct qzSingle{ static var predicate:dispatch_once_t = 0; static var instance:ZWDBManager? = nil } //保证单例只创建一次 dispatch_once(&qzSingle.predicate,{ qzSingle.instance = ZWDBManager() }) return qzSingle.instance! } //构造方法中对数据库进行创建并打开 init(){ var path:String = NSHomeDirectory().stringByAppendingString("/Documents/MCA.db") lock = NSLock() dataBase = FMDatabase(path:path) if dataBase!.open(){ var createImgTableSql:String = "create table if not exists ImgInfo(Id integer primary key autoincrement,picName varchar(256),picPath varchar(256),FID varchar(256),userID varchar(256))" //在这里要传入两个参数:第一个为创建表的sql,第二个为多参数(若第二个参数为空,则[]里面为空;若是多个参数则将对象存入参数数组,将整个数组当做第二个参数传入) var isSuccessed:Bool = dataBase!.executeUpdate(createImgTableSql,withArgumentsInArray: []) if isSuccessed { println("创建图片信息表成功!") }else{ println(dataBase!.lastErrorMessage()) } //创建报修单表 var createFixTableSql:String = "create table if not exists FixInfo(Id integer primary key autoincrement,UserName varchar(256),TelNum varchar(256),City varchar(256),Address varchar(256),YuYueDate varchar(256),ProName varchar(256),ModelNum varchar(256),BuyDate varchar(256),Desc varchar(256),FileName varchar(256),FileExt varchar(256),UserID varchar(256))" var isFixSuccessed:Bool = dataBase!.executeUpdate(createFixTableSql,withArgumentsInArray: []) if isFixSuccessed{ println("创建报修单表成功!") }else{ println(dataBase!.lastErrorMessage()) } //创建切片表 var createPieTableSql:String = "create table if not exists PieInfo(Id integer primary key autoincrement,PieNum varchar(256),PieCount varchar(256),PieLength varchar(256),PieState varchar(256),PieCheckCode varchar(256))" var isPicSuccessed:Bool = dataBase!.executeUpdate(createPieTableSql,withArgumentsInArray: []) if isPicSuccessed{ println("创建切片表成功!") }else{ println(dataBase!.lastErrorMessage()) } //创建联系人表 var createConTableSql:String = "create table if not exists ConInfo(Id integer primary key autoincrement,UserID varchar(256),UserName varchar(256),UserPhone varchar(256),UserLoc varchar(256),UserJie varchar(256))" var isConSuccessed:Bool = dataBase!.executeUpdate(createConTableSql,withArgumentsInArray: []) if isConSuccessed{ println("创建联系人表成功!") }else{ println(dataBase!.lastErrorMessage()) } } } //数据库增 func insertImgWithModel(model:ZWImageModel?){ lock!.lock() var insertSql:String = "insert into ImgInfo(picName,picPath,FID,userID) values(?,?,?,?)" var isSuccessed:Bool = dataBase!.executeUpdate(insertSql,withArgumentsInArray: [model!.picName!,model!.picPath!,model!.FID!,model!.userID!]) if isSuccessed{ println("插入数据库成功!") }else{ println(dataBase!.lastErrorMessage) } lock!.unlock() } //删除 func deleteWithPath(path:String?){ lock!.lock() var deleteSql:String = "delete from ImgInfo where picPath = ?" var isSuccessed:Bool = dataBase!.executeUpdate(deleteSql,withArgumentsInArray: [path!]) if isSuccessed{ println("删除图片成功") }else{ println(dataBase!.lastErrorMessage) } lock!.unlock() } //查询 func fetchImagsWithFID(FID:String?)->NSArray{ lock!.lock() var sql:String = "select * from ImgInfo where FID = ?" var rs:FMResultSet = dataBase!.executeQuery(sql,withArgumentsInArray: [FID!]) var array:NSMutableArray = NSMutableArray() var i:Int = 0 while rs.next(){ var model:ZWImageModel = ZWImageModel() model.picName = rs.stringForColumn("picName") model.picPath = rs.stringForColumn("picPath") model.FID = rs.stringForColumn("FID") model.userID = rs.stringForColumn("userID") array.addObject(model) i++ } lock!.unlock() return array } }
Swift中FDMB的使用(增、删、改、查),布布扣,bubuko.com
原文地址:http://blog.csdn.net/zhiwei_qin/article/details/30480387