作者:jingjing20111201 | 来源:互联网 | 2023-05-19 14:17
开发环境:Swift2.3,IOS8.0+,XCode8.2导入SQLite.swiftsourcehttps:github.comCocoaPodsSpec
开发环境: Swift2.3 , IOS8.0+ ,XCode8.2
导入SQLite.swift
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
target 'sql' do
pod 'SQLite.swift', '~> 0.10.1'
end
应用实例
import UIKit
import SQLite
public class SearchHistory: NSObject {
var db:Connection!
var searchTable:Table!
var searchKey:Expression!
var searchId:Expression!
override init() {
super.init()
connectAndCreate()
}
private func connectAndCreate() {
do {
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
db = try Connection("\(path)/db.sqlite3")
searchTable = Table("videoSearchs")
searchId = Expression("id")
searchKey = Expression("searchKey")
do {
try db.run(searchTable.create (ifNotExists:true){ t in
t.column(searchId, primaryKey: true)
t.column(searchKey, unique:true)
})
} catch {
print(error)
}
} catch {
print(error)
}
}
public func add(keyString:String) {
let insert = searchTable.insert(searchKey <- keyString)
do {
let r = try db.run(insert)
print(r)
} catch {
print(error)
}
}
public func removeAll() {
do {
try db.run(searchTable.delete())
} catch {
print(error)
}
}
public func select(limit:Int = 10) -> [String] {
var r = [String]()
do {
let list = try db.prepare(searchTable.order(searchId.desc).limit(limit))
for l in list {
let item = l[searchKey]
print(item)
r.append(item)
}
} catch {
print(error)
}
return r
}
}
参考:
- http://blog.csdn.net/sps900608/article/details/51483738