标签:
// 创建数据库 // 文件路径 : // 1.获取数据库的存放路径(沙盒中) let filePath = "/Users/xiaomage/Desktop/123.sqlite" let cString = filePath.cStringUsingEncoding(NSUTF8StringEncoding)! // 2.定义数据库对象(后面还要用,定义一个属性替代) // var db : COpaquePointer = nil // 3.打开/创建 数据库对象 if sqlite3_open(cString, &db) == SQLITE_OK { print("创建/打开数据库成功") } else { print("失败") } }
CREATE TABLE IF NOT EXISTS ‘表名‘ (
‘字段名‘ 类型(INTEGER, REAL, TEXT, BLOB)
NOT NULL 不允许为空
PRIMARY KEY 主键
AUTOINCREMENT 自增长,
‘字段名2‘ 类型,
...
DROP TABLE IF EXISTS ‘表名‘;
1 // 1.获取要执行的SQL语句 2 let createTableSQL = "DROP TABLE IF EXISTS t_student;" 3 4 // 2.执行语句 5 if sqlite3_exec(db, createTableSQL.cStringUsingEncoding(NSUTF8StringEncoding)!, nil, nil, nil) == SQLITE_OK { 6 print("删除表成功") 7 } else { 8 print("删除表失败") 9 } 10 }
class SQLiteManager { // 设计单例对象 static let shareInstance : SQLiteManager = SQLiteManager() // 数据库对象 var db : COpaquePointer = nil } // MARK:- 打开数据库的操作 extension SQLiteManager { func openDB(filePath : String) -> Bool { // 1.将Swift字符串转成C语言的字符串 let cString = filePath.cStringUsingEncoding(NSUTF8StringEncoding)! // 3.打开/创建数据库对象 return sqlite3_open(cString, &db) == SQLITE_OK } } // MARK:- 执行SQL语句 extension SQLiteManager { func execSQL(sqlString : String) -> Bool { // 1.将Swift字符串转成C语言的字符串 let cSQLString = sqlString.cStringUsingEncoding(NSUTF8StringEncoding)! // 2.执行语句 return sqlite3_exec(db, cSQLString, nil, nil, nil) == SQLITE_OK } }
INSERT INTO ‘t_student‘ (name, age, height) VALUES (‘why‘, 18, 1.88);
// 1.插入数据(获取插入语句) let insertSQL = "INSERT INTO t_student (name, age, height) VALUES (‘why‘, 18, 1.88);" // 2.执行语句 SQLiteManager.shareInstance.execSQL(insertSQL)
UPDATE ‘t_student‘ SET 字段 = ‘值‘ WHERE 条件判断;
// 1.获取更新语句 let updateSQL = "UPDATE t_student SET name = ‘yz‘;" // 2.执行语句 SQLiteManager.shareInstance.execSQL(updateSQL)
DELETE FROM t_student;
// 1.获取删除语句 let deleteSQL = "DELETE FROM t_student;" // 2.执行语句 SQLiteManager.shareInstance.execSQL(deleteSQL)
// 模拟从网络服务器中请求到很多数据(实际中数据是网络来的,这里自己写模拟) for _ in 0..<100 { let name = "zs\(arc4random_uniform(100))" let age = Int(10 + arc4random_uniform(10)) let height = 1 + Double(arc4random_uniform(10)) / 10.0 let stu = Student(name: name, age: age, height: height) stus.append(stu) }
1 class Student: NSObject { 2 var name : String = "" 3 var age : Int = 0 4 var height : Double = 0.0 5 6 init(name : String, age : Int, height : Double) { 7 self.name = name 8 self.age = age 9 self.height = height 10 } 11 } 12 13 extension Student { 14 func insertDB() { 15 // 1.插入数据(获取插入语句) 16 let insertSQL = "INSERT INTO t_student (name, age, height) VALUES (‘\(name)‘, \(age), \(height));" 17 // 2.执行语句 18 SQLiteManager.shareInstance.execSQL(insertSQL) 19 } 20 }
for stu in stus { stu.insertDB() }
1 dispatch_async(dispatch_get_global_queue(0, 0)) { 2 //获取插入数据开始时间 3 let startTime = CACurrentMediaTime() 4 // 开启事务 5 let startSQL = "BEGIN TRANSACTION;" 6 SQLiteManager.shareInstance.execSQL(startSQL) 7 8 for stu in self.stus { 9 stu.insertDB() 10 } 11 //关闭事务 12 let commitSQL = "COMMIT TRANSACTION;" 13 SQLiteManager.shareInstance.execSQL(commitSQL) 14 //获取插入数据结束时间 15 let endTime = CACurrentMediaTime() 16 //获取插入数据耗时 17 print(endTime - startTime) 18 } 19 }
1.查询语句
1 // 1.获取查询语句 2 let querySQL = "SELECT * FROM t_student LIMIT 30, 30;"; 3 4 // 2.执行语句 (执行语句封装到了一个方法里面) 5 guard let dictArray = SQLiteManager.shareInstance.querySQL(querySQL) else { 6 return 7 } 8 9 // 3.遍历数组 10 var tempArray = [Student]() 11 for dict in dictArray { 12 // 字典转模型 13 tempArray.append(Student(dict: dict)) 14 } 15 16 for stu in tempArray { 17 print(stu.name, stu.age) 18 } 19 20 //执行语句代码实现 21 func querySQL(querySQL : String) -> [[String : NSObject]]? { 22 // 0.将Swift字符串转成C语言字符串 23 let cString = querySQL.cStringUsingEncoding(NSUTF8StringEncoding)! 24 25 // 1.定义游标(指针) 26 var stmt : COpaquePointer = nil 27 28 // 2.给游标赋值 29 // 1> 参数一: 数据库对象 30 // 2> 参数二: SQL语句 31 // 3> 参数三: 该SQL语句的长度 -1 --> 系统自动计算 32 // 4> 参数四: 游标的地址 33 guard sqlite3_prepare_v2(db, cString, -1, &stmt, nil) == SQLITE_OK else { 34 return nil 35 } 36 37 // 3.取出所有的数据 38 // 3.1.定义字典数组 39 var dictArray = [[String : NSObject]]() 40 41 // 3.2.判断是否有该条数据 42 while sqlite3_step(stmt) == SQLITE_ROW { 43 // 3.3.获取字段的个数 44 let count = sqlite3_column_count(stmt) 45 // 3.4.定义字典 46 var dict = [String : NSObject]() 47 // 3.5.遍历每一个字典 48 for i in 0..<count { 49 // 3.6.取出该列的键 50 let ckey = sqlite3_column_name(stmt, i) 51 guard let key = String(UTF8String : ckey) else { 52 continue 53 } 54 // 3.7.取出该列的值 55 let cvalue = UnsafePointer<Int8>(sqlite3_column_text(stmt, i)) 56 //将c语言字符串转成swift字符串 57 let value = String(UTF8String : cvalue) 58 59 // 3.8.将键值对放入到字典中 60 dict[key] = value 61 } 62 // 3.9.将字典放入到数组中 63 dictArray.append(dict) 64 } 65 return dictArray 66 } 67 }
private lazy var db : FMDatabase = FMDatabase(path: "/Users/lgp/Desktop/321.sqlite") // 创建FMDatabase对象 // 打开/创建数据 if db.open() { print("打开成功") } else { print("失败") }
// 1.获取创建表的语句 let createSQL = "INSERT INTO t_person (name, age, height) VALUES (‘why‘, 18, 1.88);" // 2.执行语句 db.executeUpdate(createSQL, withArgumentsInArray: nil)
1 func querySQL(querySQL : String) -> [[String : NSObject]]? { 2 // 0.判断db是否有值 db定义为属性 3 guard let db = db else { 4 return nil 5 } 6 // 1.执行查询语句 结果为集合 7 let results = db.executeQuery(querySQL, withArgumentsInArray: nil) 8 9 // 2.获取数据 10 // 2.0.定义数据 11 var dictArray = [[String : NSObject]]() 12 13 // 2.1.判断结果集中是否有内容 14 while results.next() { 15 16 let count = results.columnCount() 17 18 var dict = [String : NSObject]() 19 20 for i in 0..<count { 21 let key = results.columnNameForIndex(i) 22 let value = results.stringForColumnIndex(i) 23 24 dict[key] = value 25 } 26 dictArray.append(dict) 27 } 28 return dictArray 29 }
标签:
原文地址:http://www.cnblogs.com/xiaotian666/p/5847185.html