I need to open a sqlite file and read data from that file to show in a table. So far I have a button that trigger NSOpenPanel(), and get the path. After that I tried to open the file but I have an encoding problem.
Below my code in the AppDelegate.swift
@IBAction func importFromKobo(sender: AnyObject) {
var openPanel = NSOpenPanel()
let fileDB = "kobodbtest.sqlite"
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = true
openPanel.canCreateDirectories = true
openPanel.canChooseFiles = false
openPanel.beginWithCompletionHandler { (result) -> Void in
if result == NSFileHandlingPanelOKButton {
//Do what you will
//If there's only one URL, surely 'openPanel.URL'
var deviceURL = openPanel.URL!
println("Path: \(deviceURL)")
println(self.resultDev.stringValue)
self.resultDev.stringValue += "\nPath: \(deviceURL)"
self.resultInput.stringValue += "\nPath: \(deviceURL)"
var fullFilePathURL = deviceURL.URLByAppendingPathComponent(fileDB)
self.resultInput.stringValue += "\nFull path file: \(fullFilePathURL)"
var fileOpenError:NSError?
if NSFileManager.defaultManager().fileExistsAtPath(deviceURL.path!) {
println("file OK")
if let fileContent = String(contentsOfURL: fullFilePathURL, encoding: NSUTF8StringEncoding, error: &fileOpenError) {
println(fileContent) // prints ReadMe.txt contents if successful
} else {
if let fileOpenError = fileOpenError {
println(fileOpenError) // Error Domain=NSCocoaErrorDomain Code=XXX "The file “ReadMe.txt” couldn’t be opened because...."
}
}
} else {
println("file not found")
}
}
if result == NSFileHandlingPanelCancelButton {
self.resultDev.stringValue = "Cancel"
}
}
}`
And the error in the console:
Path: file:///Users/alvaroruiz/Documents/xcode/
Result
file OK
Error Domain=NSCocoaErrorDomain Code=261 "The file “kobodbtest.sqlite” couldn’t be opened using text encoding Unicode (UTF-8)." UserInfo=0x60000026cb00 {NSFilePath=/Users/alvaroruiz/Documents/xcode/kobodbtest.sqlite, NSStringEncoding=4}
I have checked usign sqlite> PRAGMA encoding; that the format is UTF-8.
Checked with other encoding like NSISOLatin1StringEncoding and get the text but with a lot of symbols.
Could you also point me how to do the query command? and display in a table?
Thanks. Alvaro
Aucun commentaire:
Enregistrer un commentaire