此文只是讲解一下 ios下sqlite3的最基本用法(创建表、增、删、改、查)的功能。
创DB管理类
import UIKit
import SQLite3
class SQManger: NSObject {
var db:OpaquePointer? = nil
static let instance = SQManger()
class func shareInstence() -> SQManger {
return instance
}
func openDB() -> Bool {
let filePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last!
print(filePath)
let file = filePath + "/test.sqlite"
let cfile = file.cString(using: String.Encoding.utf8)
let state = sqlite3_open(cfile,&db)
if state != SQLITE_OK{
print("打开数据库失败")
return false
}
//创建表
return creatTable()
}
func creatTable() -> Bool {
let sql = "CREATE TABLE IF NOT EXISTS 't_student' ('id' integer NOT NULL PRIMARY KEY AUTOINCREMENT,'name' text,'age' integer );"
return execSql(sql: sql)
}
func execSql(sql:String) -> Bool {
let csql = sql.cString(using: String.Encoding.utf8)
return sqlite3_exec(db, csql, nil, nil, nil) == SQLITE_OK
}
func updataData(sql:String) -> Bool {
let csql = (sql.cString(using: String.Encoding.utf8))!
if sqlite3_exec(db,csql, nil, nil, nil) != SQLITE_OK {
return false
}
return true
}
func deleteData(sql:String) {
if SQManger.shareInstence().execSql(sql: sql) {
print("删除成功")
}else{
print("删除失败")
}
}
func querySql(sql:String) -> [Any]? {
var stmt:OpaquePointer? = nil
let csql = (sql.cString(using: String.Encoding.utf8))!
if sqlite3_prepare(db, csql, -1, &stmt, nil) != SQLITE_OK {
print("未准备好")
return nil
}
//准备好
var tempArr = [Any]()
while sqlite3_step(stmt) == SQLITE_ROW {
let name = String.init(cString: sqlite3_column_text(stmt, 0)!)
let age = String.init(cString: sqlite3_column_text(stmt, 1)!)
let model = Student.init(name: name, age: Int(age) ?? 0)
tempArr.append(model)
}
return tempArr
}
func querySql(kdyWords:String) -> [Any]? {
var stmt:OpaquePointer? = nil
let sql = "SELECT name,age FROM t_student WHERE name LIKE '%\(kdyWords)%' OR age LIKE '%\(kdyWords)%';"
let csql = (sql.cString(using: String.Encoding.utf8))!
if sqlite3_prepare(db, csql, -1, &stmt, nil) != SQLITE_OK {
print("未准备好")
return nil
}
//准备好
var tempArr = [Any]()
while sqlite3_step(stmt) == SQLITE_ROW {
let name = String.init(cString: sqlite3_column_text(stmt, 0)!)
let age = String.init(cString: sqlite3_column_text(stmt, 1)!)
let model = Student.init(name: name, age: Int(age) ?? 0)
tempArr.append(model)
}
return tempArr
}
}
打开数据库(如果不存在表则会新建表)
//保证数据库长链接,启动即打开数据库
if SQManger.shareInstence().openDB() {
print("数据库打开成功")
}
增
//增
@IBAction func add(_ sender: UIButton) {
//模拟新增100条数据
for i in 0..<100 {
let stu = Student(name: "张三\(i)", age: Int(arc4random() % 100))
stu.insert()
}
}
删
//删出名字叫张三3的数据 (删除所有数据)
@IBAction func deleteData(_ sender: UIButton) {
// let deleSql = "DELETE FROM t_student WHERE name = '张三3';"
let deleSql = "DELETE FROM t_student;"
SQManger.shareInstence().deleteData(sql: deleSql)
self.tableview.reloadData()
}
改
@IBAction func modeification(_ sender: UIButton) {
let sql = "UPDATE t_student SET name = '西门庆' WHERE age > 80;"//将年龄大于80的人的名字修改为 西门庆
if SQManger.shareInstence().updataData(sql: sql) {
print("更新成功")
}else{
print("更新失败")
}
}
查
//查询所有数据
@IBAction func query(_ sender: UIButton) {
let arr = Student.loadData()
addData(arr: arr)
}
//根据关键字查询
func queryOfKeyWord(ksyWord:String) {
let arr = SQManger.shareInstence().querySql(kdyWords: searchText)
addData(arr: arr)
}
网友评论