iOS數據庫:canDB.swift
canDB.swift 是一個框架,作用類似 nonSQL 的數據庫,但運作在 sqlite(FMDB) 。
canDB.swift uses sqlite (via FMDB) but it works like a nonSQL database. Just put the json into the can and retrieve it as a dictionary. Easy as pie.
// loading the json let filePath = NSBundle.mainBundle().pathForResource("data", ofType:"json") let data = NSData(contentsOfFile:filePath!, options:NSDataReadingOptions.DataReadingUncached, error:nil) let dataArray:Array = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.allZeros, error: nil) as! Array<Dictionary<String, String>>// singleton instance let storeInstance = canDB.sharedInstance // saving the data, the can is automatically created if not exists storeInstance.saveData("Person", data: dataArray, idString: kCanDBDefaultIdString, error: nil) // adding the index for future queries and reindexing the table storeInstance.addIndex("Person", indexes: ["Name"], error: nil) storeInstance.reIndex("Person", idString: kCanDBDefaultIdString) let result = storeInstance.loadData("Person") for item in result { for (key, value) in (item as! NSDictionary) { println("\(key): \(value)") } } // custom query using the previous created index "Name" let resultWithQuery = storeInstance.loadDataWithQuery("SELECT * FROM Person WHERE Name='John'") for item in resultWithQuery { for (key, value) in (item as! NSDictionary) { println("\(key): \(value)") } } storeInstance.removeDataForId("Person", idString: kCanDBDefaultIdString, idsToDelete: ["17", "19"], error: nil)</pre><br />
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!