CoreData實踐(三)——插入資料並使用SQLite Professional檢視
在學會了如何在Xcode中設計資料庫的結構之後,我們就要程式碼實現插入一條資料。
(1)程式碼實現如下:
import UIKit
import CoreData
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var row:AnyObject = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context!)
row.setValue("Robert", forKey: "name")
row.setValue(23, forKey: "age")
context?.save(nil)
println("Run Here")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
(2)注意,一定要匯入CoreData。
(3)在電腦上安裝SQLite Professional,用於檢視SQLite資料庫。我已經放到雲盤上,大家可以下載 http://pan.baidu.com/s/1ntEeJup 。然後安裝就可以 了。
(4)在AppDelegate.swift的自動生成的一個方法中,使用println()方法列印url,可以找到程式生成的資料庫檔案的位置:
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("UsingData.sqlite")
println(url)
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
coordinator = nil
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
return coordinator
}()
(5)然後列印出的地址中會有三個資料庫檔案,使用SQLite Professional開啟即可。裡面就可以看到表中的資料了。是不是很方便呢?
github主頁:https://github.com/chenyufeng1991 。歡迎大家訪問!
相關文章
- 使用 Python 字典向 SQLite 插入資料PythonSQLite
- SQLite Expert Professional資料庫開發管理SQLite資料庫
- 使用Mybatis批量插入大量資料的實踐MyBatis
- Swift實踐:使用CoreData儲存多種資料類的通訊錄Swift
- 資料儲存:CoreData
- 資料庫檢視的使用資料庫
- 資料視覺化實踐視覺化
- Python資料庫程式設計全指南SQLite和MySQL實踐Python資料庫程式設計SQLiteMySql
- Android 原生 SQLite 資料庫的一次封裝實踐AndroidSQLite資料庫封裝
- Android 中使用 SQLite 資料庫AndroidSQLite資料庫
- 使用tcpdump檢視原始資料包TCP
- CoreData的使用
- React Native列表檢視FlatList使用優化實踐指南React Native優化
- 使用MySqlBulkLoader批量插入資料MySql
- 檢視oracle資料庫真實大小Oracle資料庫
- Linux實時檢視Java介面資料LinuxJava
- iOS資料持久化儲存-CoreDataiOS持久化
- 檢視oracle資料庫的連線數以及使用者檢視Oracle資料庫
- CoreData:使用CoreData完成一個通訊錄儲存
- mybatis插入資料、批量插入資料MyBatis
- 如何使用Journalctl檢視並操作Systemd日誌
- 資料庫檢視資料庫
- 檢視資料字典
- 資料庫-檢視資料庫
- EXcel 資料檢視Excel
- 資料視覺化之下發圖實踐視覺化
- 視訊:豆瓣資料架構實踐DX架構
- MySQL 如何實現資料插入MySql
- Python 利用pymysql和openpyxl操作MySQL資料庫並插入Excel資料PythonMySql資料庫Excel
- WebSocket原理與實踐(三)--解析資料幀Web
- 記一次資料、邏輯、檢視分離的原生JS專案實踐JS
- Oracle OCP(27):使用資料字典檢視管理物件Oracle物件
- 爬蟲雙色球所有的歷史資料並儲存到SQLite爬蟲SQLite
- 使用 【Ado.Net】 批量插入資料
- 19. 使用MySQL之插入資料MySql
- 鑲嵌資料集工具小結(三)概檢視/OverviewsView
- laravel 檢視資料共享Laravel
- Vue全家桶+Echarts資料視覺化實踐VueEcharts視覺化
- 2.12 資料庫資料字典檢視資料庫