可以使用sqlite.swift框架實現與SWIFT 這裏的數據庫連接是鏈接https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md
這裏是例如連接SQLite數據庫
var db : Database!
var cat = Expression<String>("ZCATEGORY")
var name = Expression<String>("ZNAME")
var user : Query!
var stmt : Query!
var data : Array<Row>!
@IBOutlet var tabe: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
createdb()
}
internal func createdb()
{
// bundle database within your app
let path = NSBundle.mainBundle().pathForResource("db", ofType: "sqlite")
println(path)
db = Database(path, readonly: true)
// ZFOOD is table name
user = db["ZFOOD"]
println(db)
// select distinct(cat) from ZFOOD
stmt = user.select(distinct : cat)
// Stored query result in array
data = Array(stmt)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: AnyObject = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel.text = data[indexPath.row][cat]
return cell as UITableViewCell
}
我希望這有助於您
喜。感謝您的回覆。我真的只是在不使用任何包裝的情況下自己調用sql庫。我看過其他職位,但他們似乎都創建了表格和輸入值。我已經擁有了一個我不想修改的數據庫。我只想訪問表中的值。例如,表格有兩列。我只想把第一列作爲關鍵字,第二列作爲值。我一直在這個很長一段時間沒有運氣... – alionthego 2015-04-06 10:26:42
如果你使用sqlite.swift包裝,然後我會幫你,因爲我從來沒有使用SQL庫 – 2015-04-06 10:31:54
代碼更新檢查它 – 2015-04-06 10:37:51