import UIKit
class SqliteTools: NSObject {
let dbPath:String
let dbBase:FMDatabase
//使用单例创建数据库
class func sharedSocketTools()->SqliteTools{
struct YRSingleton{
static var predicate:dispatch_once_t = 0
static var instance:SqliteTools? = nil
}
dispatch_once(&YRSingleton.predicate,{
YRSingleton.instance=SqliteTools()
}
)
return YRSingleton.instance!
}
// MARK: >> 创建数据库,打开数据库
override init() {
let documentsFolder:String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let path = documentsFolder.stringByAppendingString("test.sqlite")
self.dbPath = path
//创建数据库
dbBase = FMDatabase(path: self.dbPath as String)
print("path: ---- \(self.dbPath)")
//打开数据库
if dbBase.open(){
let userSql:String = "CREATE TABLE IF NOT EXISTS t_user(id INTEGER PRIMARY KEY AUTOINCREMENT,uid TEXT, password TEXT, isRemember integer, isAutoLogin integer)"
if dbBase.executeUpdate(userSql, withArgumentsInArray: nil){
print("数据库创建成功!")
}else{
print("数据库创建失败!failed:\(dbBase.lastErrorMessage())")
}
}else{
print("Unable to open database!")
}
}
// MARK: >> 增加新用户
func addPerson(user:UserInfor) {
dbBase.open();
let arr:[AnyObject] = [user.uid!,
user.password!,
user.isRemember!,
user.isAutoLogin!];
if !self.dbBase.executeUpdate("insert into t_user (uid ,password, isRemember,isAutoLogin) values (?, ?, ?, ?)",
withArgumentsInArray: arr) {
print("添加1条数据失败!: \(dbBase.lastErrorMessage())")
}else{
print("添加1条数据成功!: \(user.uid)")
}
dbBase.close();
}
// MARK: >> 删除一个用户
func deletePerson(user:UserInfor) {
dbBase.open();
if !self.dbBase.executeUpdate("delete from t_user where uid = (?)", withArgumentsInArray: [user.uid!]) {
print("删除1条数据失败!: \(dbBase.lastErrorMessage())")
}else{
print("删除1条数据成功!: \(user.uid)")
}
dbBase.close();
}
// MARK: >> 修改用户信息
func updatePerson(user:UserInfor) {
dbBase.open();
let arr:[AnyObject] = [user.password!,
user.isRemember!,
user.isAutoLogin!,
user.uid!];
if !self.dbBase .executeUpdate("update t_user set password = (?), isRemember = (?),isAutoLogin = (?) where uid = (?)", withArgumentsInArray:arr) {
print("修改1条数据失败!: \(dbBase.lastErrorMessage())")
}else{
print("修改1条数据成功!: \(user.uid)")
}
dbBase.close();
}
// MARK: >> 查询所有用户信息
func selectPersons() -> Array<UserInfor> {
dbBase.open();
var persons=[UserInfor]()
if let rs =
dbBase.executeQuery("select uid,password,isRemember,isAutoLogin from t_user",withArgumentsInArray: nil) {
while rs.next() {
let uid:String = String(rs.stringForColumn("uid"))
let password:String = String(rs.stringForColumn("password"))
let isRemember:Int = Int(rs.intForColumn("isRemember"))
let isAutoLogin:Int = Int(rs.intForColumn("isAutoLogin"))
let user:UserInfor = UserInfor(uid: uid,
password: password,
isRemember: isRemember,
isAutoLogin: isAutoLogin)
persons.append(user)
}
} else {
print("查询失败 failed: \(dbBase.lastErrorMessage())")
}
dbBase.close();
return persons
}
//查询以 ** 开头的id
func selectUserId(id:String) -> Array<String>{
dbBase.open();
var uidAry=[String]()
if let rs =
dbBase.executeQuery("SELECT * FROM t_user WHERE uid like '\(id)%%'",withArgumentsInArray: nil){
while rs.next() {
let uid:String = String(rs.stringForColumn("uid"))
uidAry.append(uid)
}
}
dbBase.close();
return uidAry
}
// MARK: >> 删除所有用户
func deleteAllUser(){
dbBase.open();
if !self.dbBase.executeUpdate("delete from t_user",
withArgumentsInArray: nil){
print("删除1条数据失败!: \(dbBase.lastErrorMessage())")
}
dbBase.close();
}