SQLite 사용하기2 - 테이블생성, 데이터 INSERT, UPDATE, SELECT
- db.executeStatements()
- db.executeUpdate()
- db.executeQuery()
ViewController.swift)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | import UIKit class ViewController: UIViewController { //ui @IBOutlet weak var txtText: UITextView! @IBOutlet weak var btnInsert: UIButton! @IBOutlet weak var btnUpdate: UIButton! @IBOutlet weak var btnSelect: UIButton! var dbPath : String = "" let dbName : String = "test.db" let sqlCreate : String = "CREATE TABLE IF NOT EXISTS TEST ( " + " ID INTEGER PRIMARY KEY AUTOINCREMENT , " + " NAME TEXT , " + " PHONE TEXT " + ")" //데이터 입력 let sqlInsert : String = "INSERT INTO TEST (NAME, PHONE) VALUES ('권혁준' , '010-1234-1234')" //데이터 보기 let sqlSelect : String = "SELECT ID, NAME, PHONE FROM TEST ORDER BY ID" //데이터 수정 let sqlUpdate : String = "UPDATE TEST SET NAME='홍길동', PHONE='010-1234-5678' WHERE ID=?" override func viewDidLoad() { super.viewDidLoad() let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let docPath = dirPath[0] dbPath = docPath+"/"+dbName let fileManager = FileManager.default if !fileManager.fileExists(atPath: dbPath as String) { //DB 객체 생성 let database : FMDatabase? = FMDatabase(path: dbPath as String) if let db = database { //DB 열기 db.open() //TABLE 생성 db.executeStatements(sqlCreate) //DB 닫기 db.close() NSLog("TABLE 생성 성공") }else{ NSLog("DB 객체 생성 실패") } } } @IBAction func onDBInsert(_ sender: Any) { //DB 객체 생성 let database : FMDatabase? = FMDatabase(path: dbPath as String) if let db = database { //DB 열기 db.open() //INSERT db.executeUpdate(sqlInsert, withArgumentsIn: []) if db.hadError() { txtText.text = "DB INSERT 실패 \(db.lastErrorMessage())" }else{ txtText.text = "DB INSERT 성공" db.commit() } //DB 닫기 db.close() }else{ NSLog("DB 객체 생성 실패") } } @IBAction func onDBUpdate(_ sender: Any) { //DB 객체 생성 let database : FMDatabase? = FMDatabase(path: dbPath as String) if let db = database { //DB 열기 db.open() //UPDATE db.executeUpdate(sqlUpdate, withArgumentsIn: [1]) if db.hadError() { txtText.text = "DB UPDATE 실패 \(db.lastErrorMessage())" }else{ txtText.text = "DB UPDATE 성공" db.commit() } //DB 닫기 db.close() }else{ NSLog("DB 객체 생성 실패") } } @IBAction func onDBSelect(_ sender: Any) { //DB 객체 생성 let database : FMDatabase? = FMDatabase(path: dbPath as String) if let db = database { //DB 열기 db.open() //SELECT let result : FMResultSet? = db.executeQuery(sqlSelect, withArgumentsIn: []) if let rs = result { var select : String = "" while rs.next(){ select += "ID : "+rs.string(forColumn: "ID")! + ", NAME : " + rs.string(forColumn: "NAME")! + ", PHONE : " + rs.string(forColumn: "PHONE")! + "\n" } txtText.text = select }else{ txtText.text = "데이터 없음" } //DB 닫기 db.close() }else{ NSLog("DB 객체 생성 실패") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } | cs |
결과
데이터 저장
데이터 보기
데이터 수정
데이터 보기
'IT > - 프로그래밍' 카테고리의 다른 글
IOS (Swift) HTTP 통신하기1 - 특정 도메인과 HTTP 통신 허용하기 (0) | 2017.10.09 |
---|---|
Android NDK HelloWorld (0) | 2017.10.07 |
IOS (Swift) SQLite 사용하기1 - 라이브러리 적용 (0) | 2017.10.01 |
IOS (swift) 타이머 사용하여 1초마다 1씩 증가하기 (0) | 2017.09.24 |
안드로이드 SMS 수신하기 (브로드캐스트 사용) (0) | 2017.07.29 |