{"id":32295,"date":"2023-05-30T10:38:22","date_gmt":"2023-05-30T02:38:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=32295"},"modified":"2023-07-31T00:16:27","modified_gmt":"2023-07-30T16:16:27","slug":"python%e7%9a%84pymysql%e3%80%81mysql%e3%80%81mysqlx%e5%ba%93%e9%83%bd%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/32295.html","title":{"rendered":"Python\u7684pymysql\u3001mysql\u3001mysqlx\u5e93\u90fd\u6709\u4ec0\u4e48\u533a\u522b"},"content":{"rendered":"<blockquote class=\"wp-block-quote\">\n<p>Python\u7684pymysql\u3001mysql\u3001mysqlx\u5e93\u8fde\u63a5\u6570\u636e\u5e93\u6240\u4f7f\u7528\u7684\u5f15\u64ce\u4e0d\u4e00\u6837\uff0c\u7b80\u5355\u64cd\u4f5c\u90fd\u6ca1\u4ec0\u4e48\u5f71\u54cd\u7684\u3002\u5982\u679c\u4e00\u5b9a\u8981\u7528mysql\u3002\u8f83\u597d\u7528pymysql\u3002\u7eafpython\u7684\uff0c\u9ebb\u70e6\u5c11\u4e00\u70b9\u3002\u8f83\u597d\u4f7f\u7528sqlalchemy\u505a\u5916\u5c42\uff0cpymysql\u4f5c\u4e3a\u9a71\u52a8\u3002\u8fd9\u6837\u6709\u673a\u4f1a\u8f6c\u5230my sql\u3002<\/p>\n<\/blockquote>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" alt=\"\" class=\"wp-image-32296\" decoding=\"async\" height=\"421\" sizes=\"(max-width: 680px) 100vw, 680px\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/\u6dfb\u52a0\u6807\u9898-14-17.png?x-oss-process=image\/auto-orient,1\/format,webp\" srcset=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/\u6dfb\u52a0\u6807\u9898-14-17.png?x-oss-process=image\/auto-orient,1\/format,webp 680w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/\u6dfb\u52a0\u6807\u9898-14-17-300x186.png 300w\" width=\"680\"\/><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001Python\u7684pymysql\u3001mysql\u3001mysqlx\u5e93\u7684\u533a\u522b<\/strong><\/h2>\n<p>Python\u7684pymysql\u3001mysql\u3001mysqlx\u5e93\u8fde\u63a5\u6570\u636e\u5e93\u6240\u4f7f\u7528\u7684\u5f15\u64ce\u4e0d\u4e00\u6837\uff0c\u7b80\u5355\u64cd\u4f5c\u90fd\u6ca1\u4ec0\u4e48\u5f71\u54cd\u7684\u3002\u5982\u679c\u4e00\u5b9a\u8981\u7528mysql\u3002\u8f83\u597d\u7528pymysql\u3002\u7eafpython\u7684\uff0c\u9ebb\u70e6\u5c11\u4e00\u70b9\u3002\u8f83\u597d\u4f7f\u7528sqlalchemy\u505a\u5916\u5c42\uff0cpymysql\u4f5c\u4e3a\u9a71\u52a8\u3002\u8fd9\u6837\u6709\u673a\u4f1a\u8f6c\u5230my sql\u3002<\/p>\n<p>PyMySQL \u662f\u5728 Python3.x \u7248\u672c\u4e2d\u7528\u4e8e\u8fde\u63a5 MySQL \u670d\u52a1\u5668\u7684\u4e00\u4e2a\u5e93\uff0cPython2\u4e2d\u5219\u4f7f\u7528mysqldb\u3002<\/p>\n<p>PyMySQL \u9075\u5faa Python \u6570\u636e\u5e93 API v2.0 \u89c4\u8303\uff0c\u5e76\u5305\u542b\u4e86 pure-Python MySQL \u5ba2\u6237\u7aef\u5e93\u3002<\/p>\n<p>\u8fde\u63a5\u6570\u636e\u5e93\u524d\uff0c\u8bf7\u5148\u786e\u8ba4\u4ee5\u4e0b\u4e8b\u9879\uff1a<\/p>\n<ul>\n<li>\u60a8\u5df2\u7ecf\u521b\u5efa\u4e86\u6570\u636e\u5e93 TESTDB\u3002<\/li>\n<li>\u5728TESTDB\u6570\u636e\u5e93\u4e2d\u60a8\u5df2\u7ecf\u521b\u5efa\u4e86\u8868 EMPLOYEE\u3002<\/li>\n<li>EMPLOYEE\u8868\u5b57\u6bb5\u4e3a FIRST_NAME, LAST_NAME, AGE, SEX \u548c INCOME\u3002<\/li>\n<li>\u8fde\u63a5\u6570\u636e\u5e93TESTDB\u4f7f\u7528\u7684\u7528\u6237\u540d\u4e3a \u201ctestuser\u201d \uff0c\u5bc6\u7801\u4e3a \u201ctest123\u2033\uff0c\u4f60\u53ef\u4ee5\u53ef\u4ee5\u81ea\u5df1\u8bbe\u5b9a\u6216\u8005\u76f4\u63a5\u4f7f\u7528root\u7528\u6237\u540d\u53ca\u5176\u5bc6\u7801\uff0cMysql\u6570\u636e\u5e93\u7528\u6237\u6388\u6743\u8bf7\u4f7f\u7528Grant\u547d\u4ee4\u3002<\/li>\n<li>\u5728\u4f60\u7684\u673a\u5b50\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86 Python MySQLdb \u6a21\u5757\u3002<\/li>\n<\/ul>\n<p><strong>\u5ef6\u4f38\u9605\u8bfb\uff1a<\/strong><\/p>\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001MySQLdb<\/strong><\/h2>\n<p>MySQLdb\u53c8\u53ebMySQL-python \uff0c\u662f Python \u8fde\u63a5 MySQL \u7684\u4e00\u4e2a\u9a71\u52a8\uff0c\u5f88\u591a\u6846\u67b6\u90fd\u4e5f\u662f\u57fa\u4e8e\u6b64\u5e93\u8fdb\u884c\u5f00\u53d1\uff0c\u9057\u61be\u7684\u662f\u5b83\u53ea\u652f\u6301 Python2.x\uff0c\u800c\u4e14\u5b89\u88c5\u7684\u65f6\u5019\u6709\u5f88\u591a\u524d\u7f6e\u6761\u4ef6\uff0c\u56e0\u4e3a\u5b83\u662f\u57fa\u4e8eC\u5f00\u53d1\u7684\u5e93\uff0c\u5728 Windows \u5e73\u53f0\u5b89\u88c5\u975e\u5e38\u4e0d\u53cb\u597d\uff0c\u7ecf\u5e38\u51fa\u73b0\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u73b0\u5728\u57fa\u672c\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u53d6\u4ee3\u7684\u662f\u5b83\u7684\u884d\u751f\u7248\u672c\u3002<\/p>\n<p>\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p>import MySQLdb<\/p>\n<p># \u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p>db = MySQLdb.connect(\u201clocalhost\u201d, \u201ctestuser\u201d, \u201ctest123\u201d, \u201cTESTDB\u201d, charset=\u2019utf8\u2032,<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cursorclass=MySQLdb.cursors.DictCursor)<\/p>\n<p># \u4f7f\u7528cursor()\u65b9\u6cd5\u83b7\u53d6\u64cd\u4f5c\u6e38\u6807\u00a0<\/p>\n<p>cursor = db.cursor()<\/p>\n<p># \u4f7f\u7528execute\u65b9\u6cd5\u6267\u884cSQL\u8bed\u53e5<\/p>\n<p>cursor.execute(\u201cSELECT VERSION()\u201d)<\/p>\n<p># \u4f7f\u7528 fetchone() \u65b9\u6cd5\u83b7\u53d6\u4e00\u6761\u6570\u636e<\/p>\n<p>data = cursor.fetchone()<\/p>\n<p>print \u201cDatabase version : %s \u201d % data<\/p>\n<p># \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p>db.close()<\/p>\n<p>\u5c3d\u7ba1\u5b83\u662f\u57fa\u4e8eC\u5b9e\u73b0\u7684\uff0c\u901f\u5ea6\u4f1a\u5feb\u4e00\u4e9b\uff0c\u4f46\u662f\uff0c\u57fa\u4e8eC\u73af\u5883\u4f9d\u8d56\u4e5f\u662f\u5b83\u7684\u4e00\u5927\u7f3a\u70b9\uff0c\u5b83\u7684\u5b89\u88c5\u5bf9\u4e8ewindows\u7528\u6237\u53ca\u4e0d\u53cb\u597d\u3002\u800c\u4e14\u5bf9\u4e8epython3.x\u4e0d\u517c\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7684pymysql\u3001mysql\u3001mysqlx\u5e93\u8fde\u63a5\u6570\u636e\u5e93\u6240\u4f7f\u7528\u7684\u5f15\u64ce\u4e0d\u4e00\u6837\uff0c\u7b80\u5355\u64cd\u4f5c\u90fd\u6ca1\u4ec0\u4e48\u5f71\u54cd\u7684\u3002 [&hellip;]","protected":false},"author":1,"featured_media":32296,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/32295"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=32295"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/32295\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/32296"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=32295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=32295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=32295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}