2016. 10. 1. 23:34ㆍ아이폰 개발
Swift에서 SQLite 연동하는 좋은 예제가 있었는데, Swift 3로 변경되면서 몇가지 이슈가 발생하였다.
http://soooprmx.com/wp/archives/6311/comment-page-1
다음과 같이 COpaquePointer 대신 OpaquePointer로, NSFileManager 대신 FileManager로, NSURL 대신 URL로 변경을 하면 정상적으로 컴파일이 된다. 다행히 sqlite3 함수 사용부분은 손대지 않아도 된다.
var obj_db : OpaquePointer? = nil
var stmt : OpaquePointer? = nil
lazy var db_path: String = {
return self.doc_dir.appendingPathComponent("db.sqlite").path
}()
lazy var doc_dir: URL = {
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
}()
lazy var db : OpaquePointer? = {
if sqlite3_open(self.db_path, &(self.obj_db)) == SQLITE_OK {
return self.obj_db!
}
return nil
}()
예제 실행을 하면, 다음과 같이 SQLite DB 파일에 저장된 데이터를 확인할 수 있다.
[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, 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, 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]