{"id":97020,"date":"2024-10-20T14:20:21","date_gmt":"2024-10-20T06:20:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97020\/"},"modified":"2024-10-20T14:20:21","modified_gmt":"2024-10-20T06:20:21","slug":"python%e5%a6%82%e4%bd%95%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%88%e8%ae%80%e5%af%ab%ef%bc%89-python%e8%ae%80%e5%af%ab%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97020\/","title":{"rendered":"Python\u5982\u4f55\u64cd\u4f5c\u6578\u64da\u5eab\uff08\u8b80\u5beb\uff09 (python\u8b80\u5beb\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python%e5%a6%82%e4%bd%95%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%88%e8%ae%80%e5%af%ab%ef%bc%89-NvAypfmRHW\">Python\u5982\u4f55\u64cd\u4f5c\u6578\u64da\u5eab\uff08\u8b80\u5beb\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u4e14\u6613\u65bc\u5b78\u7fd2\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5eab\u4f86\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Python\u9032\u884c\u6578\u64da\u5eab\u7684\u8b80\u5beb\u64cd\u4f5c\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u3002<\/p>\n<h2 id=\"python%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%80%a3%e6%8e%a5-NvAypfmRHW\">Python\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5<\/h2>\n<p>\u5728\u958b\u59cb\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u9078\u64c7\u4e00\u500b\u5408\u9069\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u3002\u5e38\u898b\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u5305\u62ecMySQL\u3001PostgreSQL\u548cSQLite\u7b49\u3002Python\u63d0\u4f9b\u4e86\u591a\u500b\u5eab\u4f86\u652f\u6301\u9019\u4e9b\u6578\u64da\u5eab\u7684\u64cd\u4f5c\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><code>sqlite3<\/code>\uff1a\u5167\u5efa\u65bcPython\uff0c\u9069\u5408\u5c0f\u578b\u61c9\u7528\u3002<\/li>\n<li><code>MySQL Connector<\/code>\uff1a\u7528\u65bc\u9023\u63a5MySQL\u6578\u64da\u5eab\u3002<\/li>\n<li><code>psycopg2<\/code>\uff1a\u7528\u65bc\u9023\u63a5PostgreSQL\u6578\u64da\u5eab\u3002<\/li>\n<\/ul>\n<h2 id=\"%e4%bd%bf%e7%94%a8sqlite%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-NvAypfmRHW\">\u4f7f\u7528SQLite\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>SQLite\u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u6578\u64da\u5eab\uff0c\u9069\u5408\u7528\u65bc\u5c0f\u578b\u61c9\u7528\u548c\u958b\u767c\u6e2c\u8a66\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Python\u7684<code>sqlite3<\/code>\u5eab\u9032\u884c\u57fa\u672c\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u3002<\/p>\n<h3 id=\"1-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e5%ba%ab%e5%92%8c%e8%a1%a8-NvAypfmRHW\">1. \u5275\u5efa\u6578\u64da\u5eab\u548c\u8868<\/h3>\n<pre><code>import sqlite3\n\n# \u9023\u63a5\u5230\u6578\u64da\u5eab\uff08\u5982\u679c\u4e0d\u5b58\u5728\u5247\u5275\u5efa\uff09\nconn = sqlite3.connect('example.db')\n\n# \u5275\u5efa\u4e00\u500b\u6e38\u6a19\u5c0d\u8c61\nc = conn.cursor()\n\n# \u5275\u5efa\u8868\nc.execute('''CREATE TABLE IF NOT EXISTS users\n             (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)''')\n\n# \u63d0\u4ea4\u8b8a\u66f4\u4e26\u95dc\u9589\u9023\u63a5\nconn.commit()\nconn.close()<\/code><\/pre>\n<h3 id=\"2-%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-NvAypfmRHW\">2. \u63d2\u5165\u6578\u64da<\/h3>\n<pre><code>conn = sqlite3.connect('example.db')\nc = conn.cursor()\n\n# \u63d2\u5165\u6578\u64da\nc.execute(\"INSERT INTO users (name, age) VALUES ('Alice', 30)\")\nc.execute(\"INSERT INTO users (name, age) VALUES ('Bob', 25)\")\n\nconn.commit()\nconn.close()<\/code><\/pre>\n<h3 id=\"3-%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-NvAypfmRHW\">3. \u67e5\u8a62\u6578\u64da<\/h3>\n<pre><code>conn = sqlite3.connect('example.db')\nc = conn.cursor()\n\n# \u67e5\u8a62\u6578\u64da\nc.execute(\"SELECT * FROM users\")\nrows = c.fetchall()\n\nfor row in rows:\n    print(row)\n\nconn.close()<\/code><\/pre>\n<h3 id=\"4-%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-NvAypfmRHW\">4. \u66f4\u65b0\u6578\u64da<\/h3>\n<pre><code>conn = sqlite3.connect('example.db')\nc = conn.cursor()\n\n# \u66f4\u65b0\u6578\u64da\nc.execute(\"UPDATE users SET age = 31 WHERE name = 'Alice'\")\n\nconn.commit()\nconn.close()<\/code><\/pre>\n<h3 id=\"5-%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-NvAypfmRHW\">5. \u522a\u9664\u6578\u64da<\/h3>\n<pre><code>conn = sqlite3.connect('example.db')\nc = conn.cursor()\n\n# \u522a\u9664\u6578\u64da\nc.execute(\"DELETE FROM users WHERE name = 'Bob'\")\n\nconn.commit()\nconn.close()<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8mysql%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-NvAypfmRHW\">\u4f7f\u7528MySQL\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>\u5982\u679c\u9700\u8981\u8655\u7406\u66f4\u5927\u898f\u6a21\u7684\u6578\u64da\uff0cMySQL\u662f\u4e00\u500b\u4e0d\u932f\u7684\u9078\u64c7\u3002\u4f7f\u7528<code>mysql-connector-python<\/code>\u5eab\u53ef\u4ee5\u8f15\u9b06\u5730\u8207MySQL\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9dmysql-connector-NvAypfmRHW\">1. \u5b89\u88ddMySQL Connector<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88ddMySQL Connector\uff1a<\/p>\n<pre><code>pip install mysql-connector-python<\/code><\/pre>\n<h3 id=\"2-%e9%80%a3%e6%8e%a5%e5%88%b0mysql%e6%95%b8%e6%93%9a%e5%ba%ab-NvAypfmRHW\">2. \u9023\u63a5\u5230MySQL\u6578\u64da\u5eab<\/h3>\n<pre><code>import mysql.connector\n\n# \u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\nconn = mysql.connector.connect(\n    host='localhost',\n    user='yourusername',\n    password='yourpassword',\n    database='yourdatabase'\n)\n\nc = conn.cursor()<\/code><\/pre>\n<h3 id=\"3-%e5%9f%b7%e8%a1%8c%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c-NvAypfmRHW\">3. \u57f7\u884c\u57fa\u672c\u64cd\u4f5c<\/h3>\n<p>\u8207SQLite\u985e\u4f3c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>execute<\/code>\u65b9\u6cd5\u4f86\u57f7\u884cSQL\u8a9e\u53e5\uff0c\u4e26\u4f7f\u7528<code>fetchall<\/code>\u4f86\u7372\u53d6\u67e5\u8a62\u7d50\u679c\u3002<\/p>\n<pre><code># \u5275\u5efa\u8868\nc.execute('''CREATE TABLE IF NOT EXISTS users\n             (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)''')\n\n# \u63d2\u5165\u6578\u64da\nc.execute(\"INSERT INTO users (name, age) VALUES ('Charlie', 28)\")\n\n# \u67e5\u8a62\u6578\u64da\nc.execute(\"SELECT * FROM users\")\nrows = c.fetchall()\n\nfor row in rows:\n    print(row)\n\nconn.commit()\nconn.close()<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-NvAypfmRHW\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528Python\u9032\u884c\u6578\u64da\u5eab\u7684\u57fa\u672c\u8b80\u5beb\u64cd\u4f5c\uff0c\u5305\u62ecSQLite\u548cMySQL\u7684\u793a\u4f8b\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u61c9\u7528\u9084\u662f\u5927\u578b\u7cfb\u7d71\uff0cPython\u90fd\u80fd\u5920\u8f15\u9b06\u5730\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u652f\u6301\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684<code><a href=\"https:\/\/server.hk\">VPS<\/a><\/code>\u4f86\u904b\u884c\u60a8\u7684\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Python\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u5305\u62ec\u8b80\u53d6\u548c\u5beb\u5165\u6578\u64da\u7684\u57fa\u672c\u6280\u5de7\u548c\u5be6\u7528\u7bc4\u4f8b\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-97020","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97020","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=97020"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97020\/revisions"}],"predecessor-version":[{"id":97021,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97020\/revisions\/97021"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}