{"id":174861,"date":"2024-11-08T00:22:23","date_gmt":"2024-11-07T16:22:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/174861\/"},"modified":"2024-11-08T00:22:24","modified_gmt":"2024-11-07T16:22:24","slug":"%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%b7sqlite3%e6%95%b8%e6%93%9a%e5%ba%ab%e6%98%af%e5%90%a6%e5%b7%b2%e7%b6%93%e6%89%93%e9%96%8b%ef%bc%9f-sqlite3%e5%88%a4%e6%96%b7%e6%95%b8%e6%93%9a%e5%ba%ab%e6%98%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/174861\/","title":{"rendered":"\u5982\u4f55\u5224\u65b7SQLite3\u6578\u64da\u5eab\u662f\u5426\u5df2\u7d93\u6253\u958b\uff1f (sqlite3\u5224\u65b7\u6578\u64da\u5eab\u662f\u5426\u5df2\u7d93\u6253\u958b)"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%b7sqlite3%e6%95%b8%e6%93%9a%e5%ba%ab%e6%98%af%e5%90%a6%e5%b7%b2%e7%b6%93%e6%89%93%e9%96%8b%ef%bc%9f-WAkdDcPzXl\">\u5982\u4f55\u5224\u65b7SQLite3\u6578\u64da\u5eab\u662f\u5426\u5df2\u7d93\u6253\u958b\uff1f<\/h1>\n<p>SQLite\u662f\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u7531\u65bc\u5176\u7c21\u55ae\u6027\u548c\u9ad8\u6548\u6027\uff0cSQLite\u7279\u5225\u9069\u5408\u7528\u65bc\u5d4c\u5165\u5f0f\u7cfb\u7d71\u548c\u5c0f\u578b\u61c9\u7528\u3002\u7136\u800c\uff0c\u5728\u4f7f\u7528SQLite\u6642\uff0c\u958b\u767c\u8005\u5e38\u5e38\u9700\u8981\u78ba\u8a8d\u6578\u64da\u5eab\u7684\u72c0\u614b\uff0c\u7279\u5225\u662f\u5224\u65b7\u6578\u64da\u5eab\u662f\u5426\u5df2\u7d93\u6210\u529f\u6253\u958b\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5224\u65b7SQLite3\u6578\u64da\u5eab\u662f\u5426\u5df2\u7d93\u6253\u958b\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"sqlite%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%89%93%e9%96%8b%e8%88%87%e9%97%9c%e9%96%89-WAkdDcPzXl\">SQLite\u6578\u64da\u5eab\u7684\u6253\u958b\u8207\u95dc\u9589<\/h2>\n<p>\u5728\u4f7f\u7528SQLite\u6642\uff0c\u6578\u64da\u5eab\u7684\u6253\u958b\u548c\u95dc\u9589\u662f\u57fa\u672c\u64cd\u4f5c\u3002\u7576\u4f60\u4f7f\u7528SQLite\u7684API\u6253\u958b\u4e00\u500b\u6578\u64da\u5eab\u6642\uff0c\u901a\u5e38\u6703\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>import sqlite3\n\n# \u6253\u958b\u6578\u64da\u5eab\nconnection = sqlite3.connect('example.db')<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u6703\u5617\u8a66\u6253\u958b\u540d\u70ba\u201cexample.db\u201d\u7684\u6578\u64da\u5eab\u3002\u5982\u679c\u6578\u64da\u5eab\u4e0d\u5b58\u5728\uff0cSQLite\u6703\u81ea\u52d5\u5275\u5efa\u4e00\u500b\u65b0\u7684\u6578\u64da\u5eab\u6587\u4ef6\u3002<\/p>\n<h2 id=\"%e5%88%a4%e6%96%b7%e6%95%b8%e6%93%9a%e5%ba%ab%e6%98%af%e5%90%a6%e5%b7%b2%e7%b6%93%e6%89%93%e9%96%8b-WAkdDcPzXl\">\u5224\u65b7\u6578\u64da\u5eab\u662f\u5426\u5df2\u7d93\u6253\u958b<\/h2>\n<p>\u8981\u5224\u65b7SQLite\u6578\u64da\u5eab\u662f\u5426\u5df2\u7d93\u6253\u958b\uff0c\u53ef\u4ee5\u901a\u904e\u6aa2\u67e5\u9023\u63a5\u5c0d\u8c61\u7684\u72c0\u614b\u4f86\u5be6\u73fe\u3002SQLite\u7684\u9023\u63a5\u5c0d\u8c61\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u4e86\u89e3\u7576\u524d\u7684\u9023\u63a5\u72c0\u614b\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e5%b0%8d%e8%b1%a1%e7%9a%84%e9%96%89%e5%90%88%e7%8b%80%e6%85%8b-WAkdDcPzXl\">\u4f7f\u7528\u9023\u63a5\u5c0d\u8c61\u7684\u9589\u5408\u72c0\u614b<\/h3>\n<p>\u5728SQLite\u4e2d\uff0c\u9023\u63a5\u5c0d\u8c61\u6709\u4e00\u500b\u540d\u70ba\u201cclosed\u201d\u7684\u5c6c\u6027\uff0c\u9019\u500b\u5c6c\u6027\u53ef\u4ee5\u7528\u4f86\u5224\u65b7\u6578\u64da\u5eab\u662f\u5426\u5df2\u7d93\u95dc\u9589\u3002\u5982\u679c\u201cclosed\u201d\u5c6c\u6027\u8fd4\u56deTrue\uff0c\u5247\u8868\u793a\u6578\u64da\u5eab\u5df2\u7d93\u95dc\u9589\uff1b\u5982\u679c\u8fd4\u56deFalse\uff0c\u5247\u8868\u793a\u6578\u64da\u5eab\u4ecd\u7136\u6253\u958b\u3002<\/p>\n<pre><code>if connection.closed:\n    print(\"\u6578\u64da\u5eab\u5df2\u7d93\u95dc\u9589\")\nelse:\n    print(\"\u6578\u64da\u5eab\u4ecd\u7136\u6253\u958b\")<\/code><\/pre>\n<h3 id=\"%e6%8d%95%e7%8d%b2%e7%95%b0%e5%b8%b8%e4%be%86%e5%88%a4%e6%96%b7%e7%8b%80%e6%85%8b-WAkdDcPzXl\">\u6355\u7372\u7570\u5e38\u4f86\u5224\u65b7\u72c0\u614b<\/h3>\n<p>\u53e6\u4e00\u7a2e\u5224\u65b7\u6578\u64da\u5eab\u662f\u5426\u5df2\u7d93\u6253\u958b\u7684\u65b9\u6cd5\u662f\u901a\u904e\u6355\u7372\u7570\u5e38\u3002\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u5982\u679c\u6578\u64da\u5eab\u672a\u6253\u958b\uff0cSQLite\u6703\u5f15\u767c\u4e00\u500b\u201cOperationalError\u201d\u3002\u56e0\u6b64\uff0c\u6211\u5011\u53ef\u4ee5\u901a\u904e\u5617\u8a66\u57f7\u884c\u4e00\u500b\u7c21\u55ae\u7684\u67e5\u8a62\u4f86\u6aa2\u67e5\u6578\u64da\u5eab\u7684\u72c0\u614b\u3002<\/p>\n<pre><code>try:\n    cursor = connection.cursor()\n    cursor.execute(\"SELECT 1\")\n    print(\"\u6578\u64da\u5eab\u4ecd\u7136\u6253\u958b\")\nexcept sqlite3.OperationalError:\n    print(\"\u6578\u64da\u5eab\u5df2\u7d93\u95dc\u9589\u6216\u7121\u6cd5\u8a2a\u554f\")<\/code><\/pre>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-WAkdDcPzXl\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528SQLite\u6642\uff0c\u4fdd\u6301\u826f\u597d\u7684\u6578\u64da\u5eab\u7ba1\u7406\u7fd2\u6163\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\uff1a<\/p>\n<ul>\n<li>\u5728\u6bcf\u6b21\u64cd\u4f5c\u4e4b\u524d\u6aa2\u67e5\u6578\u64da\u5eab\u7684\u72c0\u614b\u3002<\/li>\n<li>\u5728\u5b8c\u6210\u64cd\u4f5c\u5f8c\uff0c\u53ca\u6642\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5\u3002<\/li>\n<li>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08with\u8a9e\u53e5\uff09\u4f86\u81ea\u52d5\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u7684\u958b\u95dc\u3002<\/li>\n<\/ul>\n<pre><code>with sqlite3.connect('example.db') as connection:\n    cursor = connection.cursor()\n    cursor.execute(\"SELECT 1\")<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u5728\u64cd\u4f5c\u5b8c\u6210\u5f8c\uff0c\u81ea\u52d5\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5\uff0c\u907f\u514d\u8cc7\u6e90\u6d6a\u8cbb\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-WAkdDcPzXl\">\u7e3d\u7d50<\/h2>\n<p>\u5224\u65b7SQLite3\u6578\u64da\u5eab\u662f\u5426\u5df2\u7d93\u6253\u958b\u662f\u958b\u767c\u904e\u7a0b\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u6b65\u9a5f\u3002\u901a\u904e\u6aa2\u67e5\u9023\u63a5\u5c0d\u8c61\u7684\u201cclosed\u201d\u5c6c\u6027\u6216\u6355\u7372\u7570\u5e38\uff0c\u6211\u5011\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u7684\u72c0\u614b\u3002\u4fdd\u6301\u826f\u597d\u7684\u6578\u64da\u5eab\u7ba1\u7406\u7fd2\u6163\u4e0d\u50c5\u80fd\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u7a69\u5b9a\u6027\uff0c\u9084\u80fd\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u904b\u884c\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u4eab\u53d7\u66f4\u5feb\u7684\u6578\u64da\u8655\u7406\u901f\u5ea6\u548c\u66f4\u9ad8\u7684\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5224\u65b7SQLite3\u6578\u64da\u5eab\u662f\u5426\u5df2\u7d93\u6253\u958b\uff0c\u638c\u63e1\u6709\u6548\u7684\u65b9\u6cd5\u4f86\u6aa2\u67e5\u6578\u64da\u5eab\u9023\u63a5\u72c0\u614b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-174861","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/174861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=174861"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/174861\/revisions"}],"predecessor-version":[{"id":174862,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/174861\/revisions\/174862"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=174861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=174861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=174861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}