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,第二个为多参数(个人理解,之前只传入sql但是一直报错,个人理解Swift中可返回多参数,传入多参数可能也如此)
var isSuccessed:Bool = dataBase!.executeUpdate(createImgTableSql,withArgumentsInArray: [])
if isSuccessed {
println("成功!")
}else{
println(dataBase!.lastErrorMessage())
}
}
}
}