xcode 如何快速使用现有的 SQLite 数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30847532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use existing SQLite database in swift?
提问by Bishan
I know there are some resources available for this but none of them clearly show the way how to do it properly.
我知道有一些可用的资源,但没有一个清楚地说明如何正确地做到这一点。
I have already populated .sqlite database (MTrader.db) and i want to connect it to my swift project and load the data from the database into spinner.
我已经填充了 .sqlite 数据库 (MTrader.db),我想将它连接到我的 swift 项目并将数据从数据库加载到微调器中。
I tried so many ways but it doesn't work. I try to edit lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator?method in AppDelegete.swift as
我尝试了很多方法,但都行不通。我尝试编辑 lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? AppDelegete.swift 中的方法为
if !NSFileManager.defaultManager().fileExistsAtPath(url.path!) {
let sourceSqliteURLs = [NSBundle.mainBundle().URLForResource("MTrader", withExtension: "db")!]
let destSqliteURLs = [self.applicationDocumentsDirectory.URLByAppendingPathComponent("MTrader.db")]
var error:NSError? = nil
for var index = 0; index < sourceSqliteURLs.count; index++ {
NSFileManager.defaultManager().copyItemAtURL(sourceSqliteURLs[index], toURL: destSqliteURLs[index], error: &error)
}
}
For some reason above code isn't working. I copied my database file in my project. So is there any error with my code or any better way to use my existing database without creating new database and populating it?
由于某种原因,上面的代码不起作用。我在我的项目中复制了我的数据库文件。那么我的代码是否有任何错误,或者有什么更好的方法可以在不创建新数据库并填充它的情况下使用我现有的数据库?
回答by BadmintonCat
First add libsqlite3.dylib to your Xcode project (in project settings/Build Phases/Link Binary with Libraries), then use something like fmdb, it makes dealing with SQLite a lot easier. It's written in Objective-C but can be used in a Swift project, too.
首先将 libsqlite3.dylib 添加到您的 Xcode 项目中(在项目设置/构建阶段/链接二进制与库中),然后使用类似 fmdb 的东西,它使处理 SQLite 变得更加容易。它是用 Objective-C 编写的,但也可以在 Swift 项目中使用。
Then you could write a DatabaseManager
class, for example...
然后你可以写一个DatabaseManager
类,例如......
import Foundation;
class DatabaseManager {
private let dbFileName = "database.db"
private var database:FMDatabase!
init() {
openDatabase()
}
func openDatabase() {
let resourcePath = NSBundle.mainBundle().resourceURL!.absoluteString
let dbPath = resourcePath?.stringByAppendingPathComponent(dbFileName)
let database = FMDatabase(path: dbPath)
/* Open database read-only. */
if (!database.openWithFlags(1)) {
println("Could not open database at \(dbPath).")
} else {
self.database = database;
}
}
func closeDatabase() {
if (database != nil) {
database.close()
}
}
func query(queryString:String) {
if let db = database, q = db.executeQuery(queryString, withArgumentsInArray: nil) {
while q.next() {
let data = q.stringForColumn("columnName")
// Do whatever here with fetched data, usually add it to an array and return that array
}
}
}
}
回答by matt
I have already populated .sqlite database
我已经填充了 .sqlite 数据库
So you probably populated it using sqlite.
所以你可能使用 sqlite 填充它。
I try to edit
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator?
我尝试编辑
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator?
But there's your problem. NSPersistentStoreCoordinate is about Core Data, not sqlite. Core Data can use a sqlite backing store, but it can't open an arbitrary sqlite database file; you're not going to be able to use a sqlite database created independently, using sqlite, with Core Data. Use sqlite itself! The easiest way is with fmdb.
但这是你的问题。NSPersistentStoreCoordinate 是关于核心数据,而不是 sqlite。Core Data 可以使用 sqlite 后备存储,但不能打开任意的 sqlite 数据库文件;您将无法使用独立创建的 sqlite 数据库,使用 sqlite 和 Core Data。使用sqlite本身!最简单的方法是使用fmdb。