{"id":96984,"date":"2024-10-20T14:01:19","date_gmt":"2024-10-20T06:01:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96984\/"},"modified":"2024-10-20T14:01:20","modified_gmt":"2024-10-20T06:01:20","slug":"python3%e4%b8%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e5%85%a8%e8%a7%a3%e6%9e%90-python3%e4%b8%8b%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96984\/","title":{"rendered":"Python3\u4e0b\u6578\u64da\u5eab\u64cd\u4f5c\u5168\u89e3\u6790 (python3\u4e0b\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python3%e4%b8%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e5%85%a8%e8%a7%a3%e6%9e%90-lnlsyeTnvo\">Python3\u4e0b\u6578\u64da\u5eab\u64cd\u4f5c\u5168\u89e3\u6790<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u6210\u70ba\u4e86\u958b\u767c\u8005\u5fc5\u5099\u7684\u6280\u80fd\u4e4b\u4e00\u3002Python3\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u4e14\u6613\u65bc\u5b78\u7fd2\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5eab\u4f86\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0ePython3\u4e0b\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u5305\u62ec\u5e38\u898b\u7684\u6578\u64da\u5eab\u985e\u578b\u3001\u9023\u63a5\u65b9\u5f0f\u3001\u57fa\u672c\u64cd\u4f5c\u53ca\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e5%b8%b8%e8%a6%8b%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e9%a1%9e%e5%9e%8b-lnlsyeTnvo\">\u5e38\u898b\u7684\u6578\u64da\u5eab\u985e\u578b<\/h2>\n<p>\u5728Python\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u6578\u64da\u5eab\u985e\u578b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u95dc\u4fc2\u578b\u6578\u64da\u5eab<\/strong>\uff1a\u5982MySQL\u3001PostgreSQL\u3001SQLite\u7b49\uff0c\u9019\u4e9b\u6578\u64da\u5eab\u4f7f\u7528\u8868\u683c\u4f86\u5b58\u5132\u6578\u64da\uff0c\u4e26\u652f\u6301SQL\u67e5\u8a62\u8a9e\u8a00\u3002<\/li>\n<li><strong>\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab<\/strong>\uff1a\u5982MongoDB\u3001Redis\u7b49\uff0c\u9019\u4e9b\u6578\u64da\u5eab\u901a\u5e38\u4ee5\u6587\u6a94\u6216\u9375\u503c\u5c0d\u7684\u5f62\u5f0f\u5b58\u5132\u6578\u64da\uff0c\u9069\u5408\u8655\u7406\u5927\u898f\u6a21\u7684\u975e\u7d50\u69cb\u5316\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-lnlsyeTnvo\">\u9023\u63a5\u6578\u64da\u5eab<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u9023\u63a5\u5230\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Python3\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import mysql.connector\n\n# \u9023\u63a5\u5230\u6578\u64da\u5eab\ndb = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"yourusername\",\n    password=\"yourpassword\",\n    database=\"yourdatabase\"\n)\n\n# \u5275\u5efa\u6e38\u6a19\ncursor = db.cursor()\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86mysql.connector\u5eab\u4f86\u9023\u63a5MySQL\u6578\u64da\u5eab\u3002\u9700\u8981\u63d0\u4f9b\u4e3b\u6a5f\u5730\u5740\u3001\u7528\u6236\u540d\u3001\u5bc6\u78bc\u548c\u6578\u64da\u5eab\u540d\u7a31\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c-lnlsyeTnvo\">\u57fa\u672c\u64cd\u4f5c<\/h2>\n<h3 id=\"%e5%89%b5%e5%bb%ba%e8%a1%a8-lnlsyeTnvo\">\u5275\u5efa\u8868<\/h3>\n<p>\u5275\u5efa\u8868\u662f\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u7b2c\u4e00\u6b65\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u4e00\u500b\u7c21\u55ae\u7528\u6236\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>create_table_query = \"\"\"\nCREATE TABLE users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    name VARCHAR(255) NOT NULL,\n    age INT NOT NULL\n)\n\"\"\"\ncursor.execute(create_table_query)\ndb.commit()\n<\/code><\/pre>\n<h3 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-lnlsyeTnvo\">\u63d2\u5165\u6578\u64da<\/h3>\n<p>\u63d2\u5165\u6578\u64da\u662f\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u6700\u5e38\u898b\u7684\u9700\u6c42\u4e4b\u4e00\u3002\u4ee5\u4e0b\u662f\u63d2\u5165\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>insert_query = \"INSERT INTO users (name, age) VALUES (%s, %s)\"\nvalues = (\"Alice\", 30)\ncursor.execute(insert_query, values)\ndb.commit()\n<\/code><\/pre>\n<h3 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-lnlsyeTnvo\">\u67e5\u8a62\u6578\u64da<\/h3>\n<p>\u67e5\u8a62\u6578\u64da\u53ef\u4ee5\u4f7f\u7528SELECT\u8a9e\u53e5\u3002\u4ee5\u4e0b\u662f\u67e5\u8a62\u6240\u6709\u7528\u6236\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>select_query = \"SELECT * FROM users\"\ncursor.execute(select_query)\n\nfor (id, name, age) in cursor:\n    print(f\"ID: {id}, Name: {name}, Age: {age}\")\n<\/code><\/pre>\n<h3 id=\"%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-lnlsyeTnvo\">\u66f4\u65b0\u6578\u64da<\/h3>\n<p>\u66f4\u65b0\u6578\u64da\u7684\u64cd\u4f5c\u4e5f\u975e\u5e38\u7c21\u55ae\uff0c\u4ee5\u4e0b\u662f\u66f4\u65b0\u7528\u6236\u5e74\u9f61\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>update_query = \"UPDATE users SET age = %s WHERE name = %s\"\nvalues = (31, \"Alice\")\ncursor.execute(update_query, values)\ndb.commit()\n<\/code><\/pre>\n<h3 id=\"%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-lnlsyeTnvo\">\u522a\u9664\u6578\u64da<\/h3>\n<p>\u522a\u9664\u6578\u64da\u7684\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528DELETE\u8a9e\u53e5\u3002\u4ee5\u4e0b\u662f\u522a\u9664\u7528\u6236\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>delete_query = \"DELETE FROM users WHERE name = %s\"\ncursor.execute(delete_query, (\"Alice\",))\ndb.commit()\n<\/code><\/pre>\n<h2 id=\"%e9%97%9c%e9%96%89%e9%80%a3%e6%8e%a5-lnlsyeTnvo\">\u95dc\u9589\u9023\u63a5<\/h2>\n<p>\u5b8c\u6210\u6240\u6709\u64cd\u4f5c\u5f8c\uff0c\u8a18\u5f97\u95dc\u9589\u6e38\u6a19\u548c\u6578\u64da\u5eab\u9023\u63a5\uff1a<\/p>\n<pre><code>cursor.close()\ndb.close()\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-lnlsyeTnvo\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86Python3\u4e0b\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u5305\u62ec\u5982\u4f55\u9023\u63a5\u6578\u64da\u5eab\u3001\u5275\u5efa\u8868\u3001\u63d2\u5165\u3001\u67e5\u8a62\u3001\u66f4\u65b0\u548c\u522a\u9664\u6578\u64da\u7b49\u57fa\u672c\u64cd\u4f5c\u3002\u638c\u63e1\u9019\u4e9b\u6280\u80fd\u5c07\u6709\u52a9\u65bc\u958b\u767c\u8005\u5728\u6578\u64da\u8655\u7406\u65b9\u9762\u66f4\u52a0\u5f97\u5fc3\u61c9\u624b\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684Python\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u89e3\u6790Python3\u4e0b\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u6db5\u84cb\u9023\u63a5\u3001\u67e5\u8a62\u3001\u66f4\u65b0\u53ca\u7ba1\u7406\u6578\u64da\uff0c\u52a9\u4f60\u8f15\u9b06\u638c\u63e1\u6578\u64da\u5eab\u61c9\u7528\u6280\u5de7\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-96984","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96984","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=96984"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96984\/revisions"}],"predecessor-version":[{"id":96985,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96984\/revisions\/96985"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}