{"id":365,"date":"2025-05-29T11:01:22","date_gmt":"2025-06-05T03:22:21","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=365"},"modified":"2025-06-05T11:01:22","modified_gmt":"2025-06-05T03:22:21","slug":"%e6%95%99%e5%ad%b8-swift-sqlite-%e7%af%84%e4%be%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e6%95%99%e5%ad%b8-swift-sqlite-%e7%af%84%e4%be%8b\/","title":{"rendered":"\u30102025 \u6700\u65b0\u7248\u3011Swift SQLite \u5b8c\u6574\u6559\u5b78\uff1a\u5f9e\u5efa\u7acb\u8cc7\u6599\u5eab\u5230\u8cc7\u6599\u64cd\u4f5c\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<h1>\u524d\u8a00<\/h1>\n<p>\u5728 Swift \u958b\u767c\u4e2d\uff0c\u4f7f\u7528 SQLite \u4f5c\u70ba\u8cc7\u6599\u5eab\u662f\u4e00\u500b\u76f8\u7576\u666e\u904d\u7684\u9078\u64c7\u3002\u96d6\u7136\u539f\u751f SQLite \u9700\u8981\u532f\u5165 .h \u6a94\u6848\u4e26\u4f7f\u7528 Objective-C \u76f8\u95dc\u7684\u7a0b\u5f0f\u78bc\uff0c\u4f46\u900f\u904e\u7b2c\u4e09\u65b9\u6846\u67b6 SQLite.swift\uff0c\u9019\u500b\u904e\u7a0b\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 SQLite\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h1>SQLite \u6b65\u9a5f\u8207\u57fa\u672c\u6587\u6cd5<\/h1>\n<p>\u5728 iOS \u61c9\u7528\u4e2d\uff0c\u6211\u5011\u5c07\u5728 Document \u6587\u4ef6\u593e\u4e0b\u5275\u5efa\u4e00\u500b\u540d\u70ba `Chuhuo.sqlite` \u7684\u8cc7\u6599\u5eab\uff0c\u4e26\u5efa\u7acb\u8207\u8cc7\u6599\u5eab\u7684\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u5efa\u7acb\u8cc7\u6599\u5eab\u7684\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"line-numbers\">let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]\nlet db = try? Connection(\"\\(path)\/Chuhuo.sqlite\")\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u8cc7\u6599\u8868\uff0c\u9019\u88e1\u6211\u5011\u4f7f\u7528 `stock` \u4f5c\u70ba\u8868\u540d\uff1a<\/p>\n<pre><code class=\"line-numbers\">let users = Table(\"stock\")\n<\/code><\/pre>\n<p>\u7136\u5f8c\u8a2d\u7f6e\u8cc7\u6599\u8868\u7684\u6b04\u4f4d\u8cc7\u6599\u683c\u5f0f\uff0c\u9664\u4e86 id \u6b04\u4f4d\u5916\uff0c\u6211\u5011\u5c07\u4f7f\u7528 String \u4f86\u9032\u884c\u6e2c\u8a66\uff1a<\/p>\n<pre><code class=\"line-numbers\">let id = Expression&lt;Int64&gt;(\"id\")\nlet price = Expression&lt;String&gt;(\"price\")\nlet product = Expression&lt;String&gt;(\"product\")\nlet date = Expression&lt;String&gt;(\"date\")\nlet amount = Expression&lt;String&gt;(\"amount\")\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u5275\u5efa\u8cc7\u6599\u8868\uff0c\u4e26\u78ba\u4fdd id \u6b04\u4f4d\u662f\u4e3b\u9375\uff0c\u4ee5\u4fbf\u65bc\u672a\u4f86\u7684\u8cc7\u6599\u5eab\u7ba1\u7406\uff1a<\/p>\n<h2>\u65b0\u589e CREATE<\/h2>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u5275\u5efa\u8cc7\u6599\u8868\uff1a<\/p>\n<pre><code class=\"line-numbers\">try! db?.run(users.create(ifNotExists: true, block: { (table) in\n    table.column(id, primaryKey: true)\n    table.column(price)\n    table.column(product)\n    table.column(date)\n    table.column(amount)\n}))\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u7b49\u540c\u65bc\u4ee5\u4e0b\u7684 SQL \u8a9e\u53e5\uff1a<\/p>\n<pre><code class=\"line-numbers\">CREATE TABLE IF NOT EXISTS \"stock\" (\n     \"id\" INTEGER PRIMARY KEY NOT NULL,\n     \"price\" TEXT,\n     \"product\" TEXT,\n     \"date\" TEXT,\n     \"amount\" TEXT\n)\n<\/code><\/pre>\n<h2>\u63d2\u5165 INSERT<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u65b0\u589e\u8cc7\u6599\u5230\u8cc7\u6599\u8868\u4e2d\u3002\u4ee5\u4e0b\u662f\u63d2\u5165\u8cc7\u6599\u7684\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"line-numbers\">do {\n    let rowid = try db?.run(users.insert(price &lt;- \"100\", product &lt;- \"car\", date &lt;- \"2025\/06\/01\", amount &lt;- \"1\"))\n    print(\"inserted id: \\(rowid!)\")\n} catch {\n    print(\"insertion failed: \\(error)\")\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u7684 SQL \u7b49\u50f9\u8a9e\u53e5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"line-numbers\">INSERT INTO stock (price, product, date, amount) VALUES ('100', 'car', '2025\/06\/01', '1')\n<\/code><\/pre>\n<h2>\u67e5\u8a62 SELECT<\/h2>\n<p>\u8981\u67e5\u8a62\u8cc7\u6599\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"line-numbers\">for user in (try? db?.prepare(users))!! {\n    print(\"id: \\(user[id]), price: \\(user[price]), product: \\(user[product])\")\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c0d\u61c9\u7684 SQL \u8a9e\u53e5\u662f\uff1a<\/p>\n<pre><code class=\"line-numbers\">SELECT * FROM stock\n<\/code><\/pre>\n<h2>\u66f4\u65b0 UPDATE<\/h2>\n<p>\u82e5\u8981\u66f4\u65b0\u8cc7\u6599\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"line-numbers\">let alice = users.filter(id == 5)\ntry? db?.run(alice.update(price &lt;- \"130\"))\n<\/code><\/pre>\n<p>\u9019\u76f8\u7576\u65bc\u4ee5\u4e0b\u7684 SQL \u8a9e\u53e5\uff1a<\/p>\n<pre><code class=\"line-numbers\">UPDATE stock SET price = '130' WHERE id = 5\n<\/code><\/pre>\n<h2>\u522a\u9664 DELETE<\/h2>\n<p>\u6700\u5f8c\uff0c\u82e5\u8981\u522a\u9664\u8cc7\u6599\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"line-numbers\">let alice = users.filter(id == 5)\ntry? db?.run(alice.delete())\n<\/code><\/pre>\n<p>\u5c0d\u61c9\u7684 SQL \u8a9e\u53e5\u70ba\uff1a<\/p>\n<pre><code class=\"line-numbers\">DELETE FROM stock WHERE id = 5\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u662f\u4f7f\u7528 SQLite \u5728 Swift \u4e2d\u9032\u884c\u57fa\u672c\u8cc7\u6599\u64cd\u4f5c\u7684\u5b8c\u6574\u6d41\u7a0b\u3002\u6211\u5011\u5df2\u7d93\u6e2c\u8a66\u904e\u6240\u6709\u7a0b\u5f0f\u78bc\uff0c\u78ba\u4fdd\u5176\u6b63\u78ba\u904b\u884c\u3002<\/p>\n<hr \/>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"Swift SQLite \u6559\u5b78\u76f8\u95dc\u5716\u7247\" title=\"\"><\/a><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. SQLite \u548c Core Data \u6709\u4ec0\u9ebc\u5340\u5225\uff1f<\/h3>\n<p>SQLite \u662f\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u8cc7\u6599\u5eab\uff0c\u800c Core Data \u662f\u4e00\u500b\u7269\u4ef6\u5716\u5f62\u548c\u6301\u4e45\u5316\u6846\u67b6\u3002SQLite \u63d0\u4f9b\u5e95\u5c64\u7684\u8cc7\u6599\u5eab\u64cd\u4f5c\uff0c\u800c Core Data \u63d0\u4f9b\u66f4\u9ad8\u5c64\u6b21\u7684\u7269\u4ef6\u7ba1\u7406\u548c\u8cc7\u6599\u6301\u4e45\u5316\u529f\u80fd\u3002<\/p>\n<h3>2. \u5982\u4f55\u8655\u7406 SQLite \u4e2d\u7684\u932f\u8aa4\uff1f<\/h3>\n<p>\u5728\u4f7f\u7528 SQLite.swift \u6642\uff0c\u5efa\u8b70\u4f7f\u7528 `do-catch` \u8a9e\u53e5\u4f86\u6355\u6349\u548c\u8655\u7406\u932f\u8aa4\uff0c\u4ee5\u4fbf\u65bc\u8abf\u8a66\u548c\u8655\u7406\u7570\u5e38\u60c5\u6cc1\u3002<\/p>\n<h3>3. SQLite \u8cc7\u6599\u5eab\u7684\u6027\u80fd\u5982\u4f55\uff1f<\/h3>\n<p>SQLite \u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u8cc7\u6599\u5eab\uff0c\u9069\u5408\u5728\u884c\u52d5\u88dd\u7f6e\u548c\u5c0f\u578b\u61c9\u7528\u4e2d\u4f7f\u7528\uff0c\u6027\u80fd\u8868\u73fe\u901a\u5e38\u76f8\u7576\u512a\u7570\uff0c\u4f46\u5728\u8655\u7406\u5927\u91cf\u8cc7\u6599\u6216\u8907\u96dc\u67e5\u8a62\u6642\uff0c\u53ef\u80fd\u6703\u51fa\u73fe\u6027\u80fd\u74f6\u9838\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>try! db?.run(users.create(ifNotExists: true, block: { (table) in<br \/>\n    table.column(id, primaryKey: true)<br \/>\n    table.column(price)<br \/>\n    table.column(product)<br \/>\n    table.column(date)<br \/>\n    table.column(amount)<br \/>\n}))<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-365","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-5T","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":590,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/365\/revisions\/590"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}