一.创建Person类
import UIKitclass Person: NSObject {var name:String? = nilvar sex:String? = nilvar age:String? = niloverride func setValue(_ value: Any?, forUndefinedKey key: String) {}}
二.在使用的地方导入sqlite.3头文件,例如方法都在viewcontroller中使用
import UIKit
import SQLite3
三.viewcontroller数据sqlite的增,删,查,等方法
//打开数据库表func opneDB() -> Bool {let docDir:String! = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).firstlet fileName:String! = docDir + "/demo1.sqlite"if sqlite3_open(fileName, &db) != SQLITE_OK {print("打开数据库失败")return false} else {print("打开数据库成功")return true}}//创建数据库表func creatTable() ->Bool {let sql = "CREATE TABLE persontable(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,name TEXT NOT NULL, age TEXT NOT NULL,sex TEXT NOT NULL);"// 第1个参数是 数据库db 第2个是要执行的sql 第3,4个是回调 第5个是错误信息let result = sqlite3_exec(db, sql, nil, nil, nil)return result == SQLITE_OK}// 插入数据func insert(nameText:String, ageText:String, sexText:String) -> Bool{let sql = "insert into persontable(name,age,sex) VALUES(?,?,?);"var stmt: OpaquePointer?let prepareResult = sqlite3_prepare_v2(db, sql, -1, &stmt, nil)if prepareResult != SQLITE_OK {sqlite3_finalize(stmt)if let msg = sqlite3_errmsg(db) {print("insert prepare 发生错误: \(msg)")}return false}let name = (nameText as NSString).utf8Stringlet age = (ageText as NSString).utf8Stringlet sex = (sexText as NSString).utf8Stringsqlite3_bind_text(stmt, 1, name, -1, nil)sqlite3_bind_text(stmt, 2, age, -1, nil)sqlite3_bind_text(stmt, 3, sex, -1, nil)let stepResult = sqlite3_step(stmt)if stepResult != SQLITE_OK && stepResult != SQLITE_DONE {sqlite3_finalize(stmt)if let msg = sqlite3_errmsg(db) {print("insert step 发生错误: \(msg)")}return false}sqlite3_finalize(stmt)return true}// 查询数据func queryAll()-> [Any]? {var persons = [Person]()//根据查询条件查询数据//let namestr:String = "张三"//let sql = "SELECT * FROM persontable WHERE name LIKE '%\(namestr)%';"let sql = "SELECT * FROM persontable ;"var stmt: OpaquePointer?// 编译let prepareResult = sqlite3_prepare_v2(db, sql, -1, &stmt, nil)if prepareResult != SQLITE_OK {// 关闭语句句柄sqlite3_finalize(stmt)if let msg = sqlite3_errmsg(db) {print("insert prepare 发生错误: \(msg)")}return []}while sqlite3_step(stmt) == SQLITE_ROW {let person = Person()//let index = String.init(cString: sqlite3_column_text(stmt, 0)!)let name = String.init(cString: sqlite3_column_text(stmt, 1)!)let age = String.init(cString: sqlite3_column_text(stmt, 2)!)let sex = String.init(cString: sqlite3_column_text(stmt, 3)!)print("查询数据----\(name)--\(age)--\(sex)")person.name = nameperson.age = ageperson.sex = sexpersons.append(person)}sqlite3_finalize(stmt)print("查询数据库成功")return persons}//更新列表func update(name: String, toName: String) -> Bool {//根据条件删除let sql = "update persontable set tradename = '\(toName)' where tradename = '\(name)'";return self.exec(sql: sql)}// 删除数据func delete(name: String) -> Bool{//删除sql语句//let sql = "delete from qingmeitable where tradename = '\(name)'"let sql = "delete from persontable "return self.exec(sql: sql)}// 执行编译sqlprivate func exec(sql: String) -> Bool{// sqlite3_stmt指针var stmt: OpaquePointer?// 编译let prepareResult = sqlite3_prepare_v2(self.db, sql, -1, &stmt, nil)if prepareResult != SQLITE_OK {// 关闭语句句柄sqlite3_finalize(stmt)if let msg = sqlite3_errmsg(db) {print("delete prepare 发生错误: \(msg)")}return false}let stepResult = sqlite3_step(stmt)if stepResult != SQLITE_OK && stepResult != SQLITE_DONE {sqlite3_finalize(stmt)if let msg = sqlite3_errmsg(db) {print("delete step 发生错误: \(msg)")}return false}sqlite3_finalize(stmt)return true}
参考链接