{"id":1581,"date":"2022-05-14T01:03:34","date_gmt":"2022-05-13T16:03:34","guid":{"rendered":"https:\/\/www.typea.info\/blog\/?p=1581"},"modified":"2022-05-14T01:04:56","modified_gmt":"2022-05-13T16:04:56","slug":"ios_sqlite_simulator","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2022\/05\/14\/ios_sqlite_simulator\/","title":{"rendered":"iOS Swift\u3067Sqlite\u3092\u4f7f\u3046\u3002\u30b7\u30df\u30e5\u30ec\u30fc\u30bf\u4e0a\u306b\u4f5c\u6210\u3055\u308c\u305fSQLite\u30d5\u30a1\u30a4\u30eb\u3092Mac\u3067\u78ba\u8a8d\u3059\u308b"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/stephencelis\/SQLite.swift\">SQLite3 \u306e Swift\u7528\u30e9\u30c3\u30d1\u30fc<\/a>\u3082\u3042\u308b\u3088\u3046\u306a\u306e\u3060\u304c\u3001\u307e\u305a\u306f<a href=\"https:\/\/www.youtube.com\/watch?v=2v0vY-u4ItA\">(265) How to store data locally in swift 5 using SQLite &#8211; YouTube<\/a>\u3092\u307f\u306a\u304c\u3089\u3001\u52d5\u4f5c\u78ba\u8a8d\u3057\u3066\u307f\u308b\u3002<\/p>\n<p>Import SQLite3 \u306e\u90e8\u5206\u3092Commnad\u62bc\u3057\u306a\u304c\u3089\u30af\u30ea\u30c3\u30af\u3001Jump to Definition \u3067\u3068\u3093\u3060\u5148\u306e\u30d8\u30c3\u30c0\u30fc\u30d5\u30a1\u30a4\u30eb\u306b\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u304c\u3042\u308b\u3002<\/p>\n<p>\u8aad\u307f\u306b\u304f\u3044\u3057\u3001XCode\u304b\u3089\u30c0\u30a4\u30ca\u30df\u30c3\u30af\u306b\u30d8\u30eb\u30d7\u53c2\u7167\u3067\u304d\u306a\u3044\u306e\u3068\u4f7f\u3044\u65b9\u304cSwift\u3061\u3063\u304f\u3067\u306a\u3044\u306e\u3067\u3001\u5148\u8ff0\u306e\u30e9\u30c3\u30d1\u30fc\u4f7f\u3063\u305f\u307b\u3046\u304c\u826f\u3055\u305d\u3046\u304b\u3002<\/p>\n<p>\u3068\u308a\u3042\u3048\u305a\u3001SQLite\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304a\u3088\u3073\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n<pre class=\"prettyprint\">import Foundation\nimport SQLite3\n\nclass DBHelper {\n    var db : OpaquePointer?\n    var path : String = \"mydb.sqlite\"\n    init() {\n        self.db = self.creatDb()\n        self.createTable()\n    }\n    func creatDb() -&gt; OpaquePointer? {\n        let filePath = try! FileManager.default.url(\n            for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)\n            .appendingPathComponent(\"mydb.sqlite\")\n        var db : OpaquePointer? = nil\n                if sqlite3_open(filePath.path, &amp;db) == SQLITE_OK {\n            let documentDirPath = NSSearchPathForDirectoriesInDomains(\n                FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)\n            print(\"Simulator location \\(documentDirPath)\")\n                        print(\"Db has been created with path = \\(filePath)\")\n            return db\n        } else {\n            print(\"Creating DB Error.\")\n            return nil\n        }\n            }\n        func createTable() {\n        let query = \"create table if not exists grade(id integer primary key autoincrement, name text, result text, list text);\"\n        var createTable : OpaquePointer? = nil\n                if sqlite3_prepare_v2(self.db, query, -1, &amp;createTable, nil) == SQLITE_OK {\n            if sqlite3_step(createTable) == SQLITE_DONE {\n                print(\"Table created\")\n            } else {\n                print(\"Table create fail\")\n            }\n                    } else {\n            print(\"Preparation fail\")\n        }\n    }\n}\n<\/pre>\n<p>ViewController\u304b\u3089\u3001\u4e0a\u8a18\u3092\u5b9f\u88c5\u3057\u305fDBHeler \u3092\u751f\u6210\u3059\u308b\u3068\u88cf\u3067DB\u3068\u30c6\u30fc\u30d6\u30eb\u3092\u751f\u6210\u3002<\/p>\n<p>\u30b7\u30df\u30e5\u30ec\u30fc\u30bf\u30fc\u306b\u751f\u6210\u3055\u308c\u305fSQLite\u30d5\u30a1\u30a4\u30eb\u306eMac\u4e0a\u306e\u5834\u6240\u3092\u30c0\u30f3\u30d7\u3059\u308b\u3002(\u30b7\u30df\u30e5\u30ec\u30fc\u30bf\u30fc\u306e\u30d1\u30b9\u306b\u30c7\u30d0\u30a4\u30b9\u306eUUID\u304c\u542b\u307e\u308c\u308b\u306e\u3067Finder\u3067\u3071\u3063\u3068\u898b\u63a2\u3057\u51fa\u305b\u306a\u3044\u306e\u3067)<\/p>\n<pre class=\"prettyprint\">import UIKit\n\nclass ViewController: UIViewController {\n        let db = DBHelper()\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \/\/ Do any additional setup after loading the view.\n    }\n}\n<\/pre>\n<p>\u5b9f\u884c\u3057\u3066\u307f\u308b\u3068\u3001SQLite\u30d5\u30a1\u30a4\u30eb\u304c\u751f\u6210\u3055\u308c\u308b\u30d1\u30b9\u306e\u30c0\u30f3\u30d7\u304a\u3088\u3073\u3001\u751f\u6210\u304c\u6b63\u5e38\u7d42\u4e86\u3057\u305f\u3088\u3046\u3060\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/05\/sqlitefilpath_on_iphone_sim.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"sqlitefilpath_on_iphone_sim.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/05\/sqlitefilpath_on_iphone_sim.png\" alt=\"sqlitefilpath_on_iphone_sim\" width=\"996\" height=\"130\" border=\"0\" \/><\/a><\/p>\n<p>Mac\u306e\u3001SQLite Studio \u3067\u4e0a\u8a18\u30d1\u30b9\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304d\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304a\u3088\u3073\u30c6\u30fc\u30d6\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/05\/sqlite_file_content.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"sqlite_file_content.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/05\/sqlite_file_content.png\" alt=\"sqlite_file_content\" width=\"569\" height=\"394\" border=\"0\" \/><\/a><\/p>\n<p>\u30e9\u30c3\u30d1\u30fc\u8a66\u3057\u3066\u307f\u3088\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQLite3 \u306e Swift\u7528\u30e9\u30c3\u30d1\u30fc\u3082\u3042\u308b\u3088\u3046\u306a\u306e\u3060\u304c\u3001\u307e\u305a\u306f(265) How to store data locally in swift 5 using SQLite &#8211; YouTube\u3092\u307f\u306a\u304c\u3089\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1580,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[127],"tags":[187,188,130],"class_list":["post-1581","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","tag-ios","tag-sqlite","tag-swift"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1581"}],"version-history":[{"count":2,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1581\/revisions"}],"predecessor-version":[{"id":1583,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1581\/revisions\/1583"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media\/1580"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}