{"id":3201,"date":"2018-03-01T12:13:50","date_gmt":"2018-03-01T04:13:50","guid":{"rendered":"http:\/\/cn.hostease.com\/xueyuan\/?p=3201"},"modified":"2018-03-01T12:13:50","modified_gmt":"2018-03-01T04:13:50","slug":"python3-4%e4%bd%bf%e7%94%a8pymysql%e8%bf%9e%e6%8e%a5mysql","status":"publish","type":"post","link":"https:\/\/cn.hostease.com\/xueyuan\/jishu\/python\/python3-4%e4%bd%bf%e7%94%a8pymysql%e8%bf%9e%e6%8e%a5mysql\/","title":{"rendered":"Python3.4\u4f7f\u7528PyMySQL\u8fde\u63a5MySQL"},"content":{"rendered":"<p>Python3\u53d1\u5e03\u4ee5\u6765\uff0c\u83b7\u53d6\u4e86\u5e7f\u5927\u7a0b\u5e8f\u5458\u4eec\u7684\u5dee\u8bc4\uff0c\u8bf4\u4e0d\u7a33\u5b9a\uff0c\u53c8\u662f\u4e0d\u517c\u5bb9\u4ec0\u4e48\u7684\uff0c\u4e0d\u8fc7\u5dee\u8bc4\u5f52\u5dee\u8bc4\uff0cPython3\u65e2\u7136\u5df2\u7ecf\u53d1\u5e03\uff0c\u80af\u5b9a\u662f\u4e2a\u8d8b\u52bf\uff0c\u4f46\u5728Python3.4\u91cc\u9762\uff0c\u4f7f\u7528\u539f\u6765Python2.7\u7684MySQLdb\u5df2\u7ecf\u4e0d\u80fd\u8fde\u63a5MySQL\u6570\u636e\u5e93\u4e86\uff0c\u6bd4\u8f83\u4ee4\u4eba\u7ea0\u7ed3\uff0c\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PyMySQL\uff0c\u6765\u5b8c\u6210\u8fde\u63a5MySQL\u7684\u91cd\u4efb\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<table class=\"table table-bordered  \">\n<tbody>\n<tr>\n<td>\u5e8f\u53f7<\/td>\n<td>\u63cf\u8ff0<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>\u53bbgithub\u4e0a\u4e0b\u8f7dpymysql\u7684\u5b89\u88c5\u5305\u00a0<a href=\"https:\/\/github.com\/PyMySQL\/PyMySQL\" target=\"_blank\" rel=\"nofollow,noindex\">pymysql<\/a><\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u89e3\u538b\u5230\u67d0\u4e2a\u76d8\u7b26\u4e0b<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>\u6253\u5f00cmd\u7a97\u53e3\uff08win\u73af\u5883\u4e0b\uff09\uff0c\u8fdb\u5165pymysql\u7684\u6839\u76ee\u5f55\u4e0b\u6267\u884c\u547d\u4ee4\uff0cpython setup.py install<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>\u5728\u7a0b\u5e8f\u91cc\uff0c\u5bfc\u5165pymysql<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>\u5f00\u59cb\u8fde\u63a5\u6570\u636e\u5e93<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6570\u636e\u5e93\u64cd\u4f5c\u7684API\u6587\u6863\u8fde\u63a5:\u00a0<a href=\"https:\/\/legacy.python.org\/dev\/peps\/pep-0249\/\" target=\"_blank\" rel=\"nofollow,noindex\">https:\/\/legacy.python.org\/dev\/peps\/pep-0249\/<\/a><\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint python\">__author__ = <span class=\"string\">'qindongliang'<\/span>\r\n<span class=\"comment\">#\u5bfc\u5165pymysql\u7684\u5305<\/span>\r\n<span class=\"keyword\">import<\/span> pymysql\r\n<span class=\"keyword\">try<\/span>:\r\n<span class=\"comment\">#\u83b7\u53d6\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u6ce8\u610f\u5982\u679c\u662fUTF-8\u7c7b\u578b\u7684\uff0c\u9700\u8981\u5236\u5b9a\u6570\u636e\u5e93<\/span>\r\nconn=pymysql.connect(host=<span class=\"string\">'localhost'<\/span>,user=<span class=\"string\">'root'<\/span>,passwd=<span class=\"string\">'qin'<\/span>,db=<span class=\"string\">'person'<\/span>,port=<span class=\"number\">3306<\/span>,charset=<span class=\"string\">'utf8'<\/span>)\r\ncur=conn.cursor()<span class=\"comment\">#\u83b7\u53d6\u4e00\u4e2a\u6e38\u6807<\/span>\r\ncur.execute(<span class=\"string\">'select * from person'<\/span>)\r\ndata=cur.fetchall()\r\n<span class=\"keyword\">for<\/span> d <span class=\"keyword\">in<\/span> data :\r\n<span class=\"comment\">#\u6ce8\u610fint\u7c7b\u578b\u9700\u8981\u4f7f\u7528str\u51fd\u6570\u8f6c\u4e49<\/span>\r\n print(<span class=\"string\">\"ID: \"<\/span>+str(d[<span class=\"number\">0<\/span>])+<span class=\"string\">'  \u540d\u5b57\uff1a '<\/span>+d[<span class=\"number\">1<\/span>]+<span class=\"string\">\"  \u6027\u522b\uff1a \"<\/span>+d[<span class=\"number\">2<\/span>])\r\n\r\ncur.close()<span class=\"comment\">#\u5173\u95ed\u6e38\u6807<\/span>\r\nconn.close()<span class=\"comment\">#\u91ca\u653e\u6570\u636e\u5e93\u8d44\u6e90<\/span>\r\n<span class=\"keyword\">except<\/span>  Exception :print(<span class=\"string\">\"\u53d1\u751f\u5f02\u5e38\"<\/span>)<\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint ruby\"><span class=\"constant\">D<\/span><span class=\"symbol\">:<\/span>\\python\\python.exe <span class=\"constant\">D<\/span><span class=\"symbol\">:\/pythonide\/pythonprojectworkspace\/python\/mysql<\/span>.py\r\n<span class=\"constant\">ID<\/span><span class=\"symbol\">:<\/span> <span class=\"number\">1<\/span>  \u540d\u5b57\uff1a \u79e6\u5929  \u6027\u522b\uff1a \u7537\r\n<span class=\"constant\">ID<\/span><span class=\"symbol\">:<\/span> <span class=\"number\">2<\/span>  \u540d\u5b57\uff1a \u738b\u6676  \u6027\u522b\uff1a \u5973\r\n\r\n<span class=\"constant\">Process<\/span> finished with exit code <span class=\"number\">0<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python3\u53d1\u5e03\u4ee5\u6765\uff0c\u83b7\u53d6\u4e86\u5e7f\u5927\u7a0b\u5e8f\u5458\u4eec\u7684\u5dee\u8bc4\uff0c\u8bf4\u4e0d\u7a33\u5b9a\uff0c\u53c8\u662f\u4e0d\u517c\u5bb9\u4ec0\u4e48\u7684\uff0c\u4e0d\u8fc7\u5dee\u8bc4\u5f52\u5dee\u8bc4\uff0cPython3\u65e2 &#8230; <a title=\"Python3.4\u4f7f\u7528PyMySQL\u8fde\u63a5MySQL\" class=\"read-more\" href=\"https:\/\/cn.hostease.com\/xueyuan\/jishu\/python\/python3-4%e4%bd%bf%e7%94%a8pymysql%e8%bf%9e%e6%8e%a5mysql\/\" aria-label=\"\u9605\u8bfb Python3.4\u4f7f\u7528PyMySQL\u8fde\u63a5MySQL\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[654],"tags":[45],"class_list":["post-3201","post","type-post","status-publish","format-standard","hentry","category-python","tag-mysql"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/3201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/comments?post=3201"}],"version-history":[{"count":1,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/3201\/revisions"}],"predecessor-version":[{"id":3202,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/3201\/revisions\/3202"}],"wp:attachment":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/media?parent=3201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/categories?post=3201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/tags?post=3201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}