import Foundation
enum SQLite3Error : Error {
case connectionError(String)
case queryError(String)
case valueError(String)
case otherError(String)
}
class SQLite3Conn {
var stmt: OpaquePointer? = nil
var db: OpaquePointer? = nil
init?() {
var needsSetup = false
let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
.last!.appendingPathComponent("data.db").path
if !FileManager.default.fileExists(atPath: path) {
print("no database file. setup.")
needsSetup = true
}
/*
func sqlite3_open(_ filename: UnsafePointer<Int8>!, _ ppDb: UnsafeMutablePointer<OpaquePointer?>!) -> Int32
*/
if sqlite3_open(path, &db) != SQLITE_OK {
return nil
}
if needsSetup {
let query = "CREATE TABLE IF NOT EXISTS test ( num INT, text TEXT );"
do {
try prepare(query: query){ () -> Void in
if sqlite3_step(stmt) != SQLITE_DONE {
throw SQLite3Error.valueError("Fail to create table")
}
}
} catch {
print("Error")
return nil
}
}
}
deinit {
if let db = db {
sqlite3_close(db)
}
}
func prepare<ResultType>(query: String, _ body: () throws -> ResultType) throws -> ResultType {
/*
func sqlite3_prepare_v2(
_ db: OpaquePointer!,
_ zSql: UnsafePointer<Int8>!,
_ nByte: Int32,
_ ppStmt: UnsafeMutablePointer<OpaquePointer?>!,
_ pzTail: UnsafeMutablePointer<UnsafePointer<Int8>?>!
) -> Int32
*/
guard sqlite3_prepare_v2(db, query, -1, &stmt, nil) == SQLITE_OK
else { throw SQLite3Error.queryError("Fail to compile Query:\n\(query)")}
defer {sqlite3_finalize(stmt)}
let r = try body()
return r
}
}
extension SQLite3Conn {
func insert(num: Int32, text: String) {
let query = "INSERT INTO test (num, text) VALUES (?, ?)"
do {
try prepare(query: query){ () -> Void in
guard sqlite3_bind_int(stmt, 1, num) == SQLITE_OK else { throw SQLite3Error.valueError("can't bind num") }
guard sqlite3_bind_text(stmt, 2, text, -1, nil) == SQLITE_OK else {throw SQLite3Error.valueError("can't bind text")}
guard sqlite3_step(stmt) == SQLITE_DONE else {throw SQLite3Error.otherError("Fail to insert")}
print("ok")
}
} catch {
print("fail")
}
}
}
let d = SQLite3Conn()!
d.insert(num: 2, text: "World")
d.insert(num: 43, text: "TEST")
d.insert(num:55, text: "Anna")
swift3 sqlite3操作
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 练习写swift代码,一直不明白as、as!、as? 什么时候使用,只是根据系统提示来修改,今天研究了下. ask...