2

I want to build a Dictionary app for iOS. I try to read data from sqlite database file with SQLite.swift here.

My code

func configureCell(){
    //Todo: read sqlite file
    //let path = Bundle.main.path(forResource: "taidict", ofType: "sqlite")!

    do{
        let path = Bundle.main.path(forResource: "taidict", ofType: "sqlite")!

        let db = try Connection(path, readonly: true)
        let users = Table("words")
        let id = Expression<Int64>("id")
        let word = Expression<String?>("word")
        let shanword_uni = Expression<String>("shanword_uni")

        for user in try db.prepare(users.select(id, word, shanword_uni)) {
            print("id: \(user[id]), word: \(user[word]), meaning: \(user[shanword_uni])")
            // id: 1, email: alice@mac.com
        }


    }catch{
        print(error)
    }

}

When I try to run, I have got an error like below.

    2017-10-03 21:59:57.826502+0630 TaiDict[60682:516343] [MC] Lazy loading NSBundle MobileCoreServices.framework
2017-10-03 21:59:57.827558+0630 TaiDict[60682:516343] [MC] Loaded MobileCoreServices.framework
2017-10-03 21:59:57.841476+0630 TaiDict[60682:516343] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/saitanwgpha/Library/Developer/CoreSimulator/Devices/BA4F6AD0-6C4D-4F29-B0E0-34B9F63607A0/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
fatal error: 'try!' expression unexpectedly raised an error: Unexpected null value for column `"id"`: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.59/src/swift/stdlib/public/core/ErrorType.swift, line 181
2017-10-03 21:59:57.870943+0630 TaiDict[60682:516343] fatal error: 'try!' expression unexpectedly raised an error: Unexpected null value for column `"id"`: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.59/src/swift/stdlib/public/core/ErrorType.swift, line 181
Message from debugger: The LLDB RPC server has crashed. The crash log is located in ~/Library/Logs/DiagnosticReports and has a prefix 'lldb-rpc-server'. Please file a bug and attach the most recent crash log.

How should I fix it? Please help me. Best, Sai Pha

SaiPha
  • 41
  • 6
  • 1
    Your error message says: *"Unexpected null value for column `"id"`"* ... so figure out why you are setting id to null. – DonMag Oct 03 '17 at 15:50
  • In my database file, I have to columns. One is the word and second is the meaning of word. The column one name is "word" and next column name is "shanword_uni". I try to read all data from that two columns. How to write the code. – SaiPha Oct 03 '17 at 15:55
  • 1
    If you only have `word` and `shanword_uni` then remove id. – MwcsMac Oct 03 '17 at 15:59
  • Yep, I've removed like your mention. But I still have got an error ' fatal error: 'try!' expression unexpectedly raised an error: Unexpected null value for column `"shanword_uni" '. How to do sir. @MwcsMac – SaiPha Oct 03 '17 at 16:10
  • 1
    Ok you need to handle the null values see [Handle Null Vales](https://stackoverflow.com/questions/37606376/how-to-handle-json-null-values-in-swift) this references json but the process is the same. – MwcsMac Oct 03 '17 at 17:41
  • Okey, thank you and will try like you said. I'll let you know soon. @MwcsMac – SaiPha Oct 04 '17 at 03:15
  • I fix my problem with changing some code like below. Thank you all. – SaiPha Oct 05 '17 at 04:13
  • `let path = Bundle.main.path(forResource: "taidict", ofType: "sqlite")! let db = try Connection(path, readonly: true) for user in try db.prepare(users) { do { let homeD = Data(word: "\(try user.get(word)!)", meaning: "\(try user.get(shanword_uni))") self.homeDict.append(homeD) }` – SaiPha Oct 05 '17 at 04:16

0 Answers0