{"id":185522,"date":"2024-11-10T22:16:20","date_gmt":"2024-11-10T14:16:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/185522\/"},"modified":"2024-11-10T22:16:21","modified_gmt":"2024-11-10T14:16:21","slug":"%e4%bd%bf%e7%94%a8%e6%8c%87%e5%8d%97%ef%bc%88pymysql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/185522\/","title":{"rendered":"\u4f7f\u7528\u6307\u5357\uff08pymysql\u6578\u64da\u5eab\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8%e6%8c%87%e5%8d%97%ef%bc%88pymysql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%89-OFmUmNRfEO\">\u4f7f\u7528\u6307\u5357\uff08pymysql\u6578\u64da\u5eab\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u548c\u64cd\u4f5c\u662f\u6bcf\u500b\u958b\u767c\u8005\u5fc5\u9808\u638c\u63e1\u7684\u6280\u80fd\u4e4b\u4e00\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5eab\u4f86\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u5176\u4e2d\u662f\u4e00\u500b\u975e\u5e38\u53d7\u6b61\u8fce\u7684\u9078\u64c7\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528\u4f86\u9023\u63a5\u548c\u64cd\u4f5cMySQL\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afpymysql%ef%bc%9f-OFmUmNRfEO\">\u4ec0\u9ebc\u662fpymysql\uff1f<\/h2>\n<p>\u662f\u4e00\u500b\u7d14Python\u5be6\u73fe\u7684MySQL\u5ba2\u6236\u7aef\uff0c\u5141\u8a31\u958b\u767c\u8005\u4f7f\u7528Python\u8a9e\u8a00\u8207MySQL\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u5b83\u652f\u6301Python 3.x\u7248\u672c\uff0c\u4e26\u4e14\u8207MySQL\u7684\u5404\u7a2e\u7248\u672c\u517c\u5bb9\u3002\u4f7f\u7528\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u57f7\u884cSQL\u67e5\u8a62\u3001\u63d2\u5165\u6578\u64da\u3001\u66f4\u65b0\u6578\u64da\u4ee5\u53ca\u522a\u9664\u6578\u64da\u7b49\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9dpymysql-OFmUmNRfEO\">\u5b89\u88ddpymysql<\/h2>\n<p>\u5728\u958b\u59cb\u4f7f\u7528\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd\u8a72\u5eab\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip install pymysql<\/code><\/pre>\n<h2 id=\"%e9%80%a3%e6%8e%a5%e5%88%b0mysql%e6%95%b8%e6%93%9a%e5%ba%ab-OFmUmNRfEO\">\u9023\u63a5\u5230MySQL\u6578\u64da\u5eab<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u63a5\u4e0b\u4f86\u9700\u8981\u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u9023\u63a5\u6578\u64da\u5eab\u7684\u57fa\u672c\u4ee3\u78bc\uff1a<\/p>\n<pre><code>import pymysql\n\n# \u9023\u63a5\u5230\u6578\u64da\u5eab\nconnection = pymysql.connect(\n    host='localhost',  # \u6578\u64da\u5eab\u4e3b\u6a5f\u5730\u5740\n    user='your_username',  # \u6578\u64da\u5eab\u7528\u6236\u540d\n    password='your_password',  # \u6578\u64da\u5eab\u5bc6\u78bc\n    database='your_database'  # \u6578\u64da\u5eab\u540d\u7a31\n)\n\n# \u78ba\u8a8d\u9023\u63a5\ntry:\n    with connection.cursor() as cursor:\n        # \u57f7\u884c\u4e00\u500b\u67e5\u8a62\n        sql = \"SELECT VERSION()\"\n        cursor.execute(sql)\n        result = cursor.fetchone()\n        print(\"Database version:\", result)\nfinally:\n    connection.close()\n<\/code><\/pre>\n<h2 id=\"%e5%9f%b7%e8%a1%8c%e5%9f%ba%e6%9c%ac%e7%9a%84sql%e6%93%8d%e4%bd%9c-OFmUmNRfEO\">\u57f7\u884c\u57fa\u672c\u7684SQL\u64cd\u4f5c<\/h2>\n<h3 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-OFmUmNRfEO\">\u63d2\u5165\u6578\u64da<\/h3>\n<p>\u63d2\u5165\u6578\u64da\u662f\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u6700\u5e38\u898b\u7684\u4efb\u52d9\u4e4b\u4e00\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u63d2\u5165\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>connection = pymysql.connect(\n    host='localhost',\n    user='your_username',\n    password='your_password',\n    database='your_database'\n)\n\ntry:\n    with connection.cursor() as cursor:\n        # \u63d2\u5165\u6578\u64da\n        sql = \"INSERT INTO users (name, age) VALUES (%s, %s)\"\n        cursor.execute(sql, ('Alice', 30))\n    connection.commit()  # \u63d0\u4ea4\u4e8b\u52d9\nfinally:\n    connection.close()\n<\/code><\/pre>\n<h3 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-OFmUmNRfEO\">\u67e5\u8a62\u6578\u64da<\/h3>\n<p>\u67e5\u8a62\u6578\u64da\u662f\u53e6\u4e00\u500b\u5e38\u898b\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u67e5\u8a62\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>connection = pymysql.connect(\n    host='localhost',\n    user='your_username',\n    password='your_password',\n    database='your_database'\n)\n\ntry:\n    with connection.cursor() as cursor:\n        # \u67e5\u8a62\u6578\u64da\n        sql = \"SELECT * FROM users\"\n        cursor.execute(sql)\n        result = cursor.fetchall()\n        for row in result:\n            print(row)\nfinally:\n    connection.close()\n<\/code><\/pre>\n<h3 id=\"%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-OFmUmNRfEO\">\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\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>connection = pymysql.connect(\n    host='localhost',\n    user='your_username',\n    password='your_password',\n    database='your_database'\n)\n\ntry:\n    with connection.cursor() as cursor:\n        # \u66f4\u65b0\u6578\u64da\n        sql = \"UPDATE users SET age = %s WHERE name = %s\"\n        cursor.execute(sql, (31, 'Alice'))\n    connection.commit()\nfinally:\n    connection.close()\n<\/code><\/pre>\n<h3 id=\"%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-OFmUmNRfEO\">\u522a\u9664\u6578\u64da<\/h3>\n<p>\u522a\u9664\u6578\u64da\u7684\u64cd\u4f5c\u4e5f\u5f88\u7c21\u55ae\uff0c\u4ee5\u4e0b\u662f\u522a\u9664\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>connection = pymysql.connect(\n    host='localhost',\n    user='your_username',\n    password='your_password',\n    database='your_database'\n)\n\ntry:\n    with connection.cursor() as cursor:\n        # \u522a\u9664\u6578\u64da\n        sql = \"DELETE FROM users WHERE name = %s\"\n        cursor.execute(sql, ('Alice',))\n    connection.commit()\nfinally:\n    connection.close()\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-OFmUmNRfEO\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528\u5eab\u4f86\u9023\u63a5\u548c\u64cd\u4f5cMySQL\u6578\u64da\u5eab\uff0c\u5305\u62ec\u57fa\u672c\u7684\u63d2\u5165\u3001\u67e5\u8a62\u3001\u66f4\u65b0\u548c\u522a\u9664\u64cd\u4f5c\u3002\u638c\u63e1\u9019\u4e9b\u57fa\u672c\u64cd\u4f5c\u5f8c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\uff0c\u4e26\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u6578\u64da\u8655\u7406\u908f\u8f2f\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684\u6578\u64da\u5eab\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u9805\u76ee\u9084\u662f\u5927\u578b\u61c9\u7528\uff0c\u60a8\u90fd\u53ef\u4ee5\u5728\u6211\u5011\u7684<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4e0a\u627e\u5230\u5408\u9069\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u4f7f\u7528\u6307\u5357\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u5730\u4f7f\u7528pymysql\u9023\u63a5\u548c\u64cd\u4f5cMySQL\u6578\u64da\u5eab\uff0c\u63d0\u5347\u6578\u64da\u7ba1\u7406\u6548\u7387\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-185522","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185522","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=185522"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185522\/revisions"}],"predecessor-version":[{"id":185523,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185522\/revisions\/185523"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=185522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=185522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=185522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}