在iOS开发里面有关数据库的第三方库以ORM居多,直接使用SQL且配置简单的第三方库少知甚少。SQLiteDB便可较为轻易地配置且可直接使用SQL语句。由于其语法和使用流程与JDBC较为接近,熟悉JDBC的读者甚至可略读一番便可直接上手。
很可惜SQLiteDB没有提供SPM版本,否则一个URL便可直接导入完成。SQLiteDB官网:
https://github.com/fahimf/sqlitedb
没有创建也不用担心。先把下载的文件解压缩,把图中的data.db文件拖动到项目工程里面,然后默认选项finish即可。这个db文件即为被操纵的数据库文件。
如图所示。
并在生成的文件里面写入如下代码:
#import "sqlite3.h"
#import
如图所示
自此,配置完毕。
// db对象
var db: SQLiteDB!
@IBAction func createListener(_ sender: Any) {
// 获取db实例对象
db = SQLiteDB.shared
// 先打开,再操作
db.open(dbPath: "", copyFile: true, inMemory: false)
let result = db.execute(sql: "create table if not exists test_user(id integer primary key,name varchar(20),mobile varchar(20))")
print(result)
}
其中,open()
语句第一个参数为数据库名称,这里为空也就是data.db;第二个参数默认为true;第三个参数为是否使用内存中的数据库,为false则使用磁盘中的数据库,即为data.db。
let sql = "insert into test_user(id,name,mobile) values(2007001,'jack','18655886666')"
let result = db.execute(sql: sql)
print(result)
let data = db.execute(sql: "delete from test_user where name = 'rose'")
print("data is \(data)")
let data = db.execute(sql: "update test_user set name = 'rose' where id = 2007001")
print("data is \(data)")
let data = db.query(sql: "select * from test_user")
if data.count > 0 {
let user = data[data.count - 1]
let id = user["id"] as? Int
let name = user["name"] as? String
let mobile = user["mobile"] as? String
print("RESULT IS: \(id!), \(name!), \(mobile!)")
}
需要注意的是,结果值为optional类型,注意解包。
import UIKit
class ViewController: UIViewController {
var db: SQLiteDB!
@IBAction func createListener(_ sender: Any) {
db = SQLiteDB.shared
db.open(dbPath: "", copyFile: true, inMemory: false)
let result = db.execute(sql: "create table if not exists test_user(id integer primary key,name varchar(20),mobile varchar(20))")
print(result)
}
@IBAction func addListener(_ sender: Any) {
let sql = "insert into test_user(id,name,mobile) values(2007002,'nancy','18655886666')"
let result = db.execute(sql: sql)
print(result)
}
@IBAction func deleteListener(_ sender: Any) {
let data = db.execute(sql: "delete from test_user where name = 'rose'")
print("data is \(data)")
}
@IBAction func alterListener(_ sender: Any) {
let data = db.execute(sql: "update test_user set name = 'rose' where id = 2007002")
print("data is \(data)")
}
@IBAction func retreieveListener(_ sender: Any) {
let data = db.query(sql: "select * from test_user")
if data.count > 0 {
//获取最后一行数据显示
let user = data[data.count - 1]
let id = user["id"] as? Int
let name = user["name"] as? String
let mobile = user["mobile"] as? String
print("RESULT IS: \(id!), \(name!), \(mobile!)")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
打印结果:
2021-03-22 16:10:02.730335+0800 TestSQL[9158:132247] DB Open called with path:
2021-03-22 16:10:02.731004+0800 TestSQL[9158:132247] Calling Super Open with path: /Users/mac/Library/Developer/CoreSimulator/Devices/3B9978F2-5DF3-45ED-9120-9107DF1A1957/data/Containers/Bundle/Application/F1A356D4-4810-4C87-B66F-16ABEE10ACF5/TestSQL.app/data.db
2021-03-22 16:10:02.733442+0800 TestSQL[9158:132247] SQLiteDB Opening DB at: /Users/mac/Library/Developer/CoreSimulator/Devices/3B9978F2-5DF3-45ED-9120-9107DF1A1957/data/Containers/Data/Application/0FA3CFBA-F17D-4A38-BDC4-444AF994EED7/Library/data.db
2021-03-22 16:10:02.734731+0800 TestSQL[9158:132247] SQLiteDB opened!
1
2007002
RESULT IS: 2007002, nancy, 18655886666
data is 1
RESULT IS: 2007002, rose, 18655886666