使用带有 ios8 应用程序的 sqlite3 数据库,使用 Xcode 6 和 swift
using an sqlite3 database with an ios8 app using Xcode 6 and swift
我对编程还很陌生。我正在使用 Xcode 6 和 swift 构建一个 iOS 8 应用程序。我的应用程序有相当多的数据,我使用 firefox 插件将这些数据组织到 sqlite3 文件中。我已将这些文件放入我的项目文件夹中。他们的扩展名是 .sql 现在我需要在我的应用程序中访问该数据。我已经通过在构建阶段选项卡中添加 libsqlite3.dylib 来链接 sqlite3 库。现在我想编写代码来访问数据库。我在网上找到了很多信息,其中包含 iOS 7 和更早版本使用 objective-c 的代码和示例,但没有关于 iOS 8 和 swift 的信息。如果有人可以帮助我或引导我朝着正确的方向前进,我将不胜感激。谢谢。
可以使用sqlite.swift框架实现与swift的数据库连接
这里是 link 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
}
希望对你有所帮助
我对编程还很陌生。我正在使用 Xcode 6 和 swift 构建一个 iOS 8 应用程序。我的应用程序有相当多的数据,我使用 firefox 插件将这些数据组织到 sqlite3 文件中。我已将这些文件放入我的项目文件夹中。他们的扩展名是 .sql 现在我需要在我的应用程序中访问该数据。我已经通过在构建阶段选项卡中添加 libsqlite3.dylib 来链接 sqlite3 库。现在我想编写代码来访问数据库。我在网上找到了很多信息,其中包含 iOS 7 和更早版本使用 objective-c 的代码和示例,但没有关于 iOS 8 和 swift 的信息。如果有人可以帮助我或引导我朝着正确的方向前进,我将不胜感激。谢谢。
可以使用sqlite.swift框架实现与swift的数据库连接 这里是 link 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
}
希望对你有所帮助