{"id":712,"date":"2017-04-12T13:46:41","date_gmt":"2017-04-12T05:46:41","guid":{"rendered":"http:\/\/192.168.1.20\/?p=712"},"modified":"2017-10-23T11:52:50","modified_gmt":"2017-10-23T03:52:50","slug":"python3-{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}e6{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}93{47dd41ec6bdcbd9a214c783a3735392b82ea6d1d918378c5a53e064","status":"publish","type":"post","link":"http:\/\/alextop.top\/?p=712","title":{"rendered":"python3 \u64cd\u4f5c\u6570\u636e\u5e93"},"content":{"rendered":"<p>\u67e5\u4e86\u4e00\u4e0bpython\u64cd\u4f5cmysql\u6570\u636e\u5e93\u7684\u5e93\u53ebpython-mysql\uff0c\u4f46\u662f\u8fd9\u4e2a\u53ef\u662f\u8fd9\u4e2a\u5e93\u53ea\u652f\u6301py2.7<\/p>\n<p>\u65b0\u7684\u5e93\u53ebpymysql\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5927\u5bb6pip\u8d77\u6765\u5427\u3002\u542c\u8bf4<a class=\"replace_word\" title=\"MySQL\u77e5\u8bc6\u5e93\" href=\"http:\/\/lib.csdn.net\/base\/mysql\" target=\"_blank\">MySQL<\/a>\u5b98\u65b9\u51fa\u4e86\u4e2a\u65b0\u7248\uff0c\u4e0d\u8fc7\u8fd8\u6ca1\u53bb\u770b\u3002<\/p>\n<p>\u6211\u8fd9\u7528\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a<\/p>\n<p>import pymysql<\/p>\n<p>conn = pymysql.connect(host=&#8217;127.0.0.1&#8242;, port=3306, user=&#8217;root&#8217;, passwd=&#8217;test&#8217;,<\/p>\n<p>db=&#8217;mysql&#8217;)<\/p>\n<p>cur = conn.cursor()<\/p>\n<p>cur.execute(&#8220;SELECT * FROM user&#8221;)<\/p>\n<p>for r in cur.fetchall():<\/p>\n<p>print(r)<\/p>\n<p>#cur.close()<\/p>\n<p>conn.close()<\/p>\n<h1><\/h1>\n<h1>\u7531\u4e8e\u6700\u8fd1\u5728\u4ece\u4e8bdjango\u5f00\u53d1\u6b21\u9519\u8bef\u4e5f\u662f\u5728win\u4e0b\u8c03\u8bd5\u5f15\u8d77\u7684\uff0c\u7279\u6b64\u8bb0\u5f55<\/h1>\n<h1><\/h1>\n<h1><\/h1>\n<h1>\u5982\u4f55\u5728Django1.6\u7ed3\u5408Python3.4\u7248\u672c\u4e2d\u4f7f\u7528MySql<\/h1>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5173\u4e8eDjango1.6\u4e2dDATABASES\u7684\u8bbe\u7f6e\u4e5f\u662f\u4e00\u6837\u4e0d\u7528\u505a\u4efb\u4f55\u4fee\u6539\uff0c\u8ddf\u4ee5\u524dMySQLdb\u7684\u65f6\u5019\u4e00\u6837\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\" target=\"_blank\"><img decoding=\"async\" src=\"http:\/\/sxfu.tk\/wp-content\/uploads\/2017\/04\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre> 1 DATABASES = {\r\n 2     'default': {\r\n 3         'ENGINE': 'django.db.backends.mysql', #\u6570\u636e\u5e93\u5f15\u64ce\r\n 4         'NAME': 'test',                       #\u6570\u636e\u5e93\u540d\r\n 5         'USER': 'root',                       #\u7528\u6237\u540d\r\n 6         'PASSWORD': 'root',                   #\u5bc6\u7801\r\n 7         'HOST': '',                           #\u6570\u636e\u5e93\u4e3b\u673a\uff0c\u9ed8\u8ba4\u4e3alocalhost\r\n 8         'PORT': '',                           #\u6570\u636e\u5e93\u7aef\u53e3\uff0cMySQL\u9ed8\u8ba4\u4e3a3306\r\n 9         'OPTIONS': {\r\n10             'autocommit': True,\r\n11         },\r\n12     }\r\n13 }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\" target=\"_blank\"><img decoding=\"async\" src=\"http:\/\/sxfu.tk\/wp-content\/uploads\/2017\/04\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u6700\u5173\u952e\u7684\u4e00\u70b9\uff0c\u5728\u7ad9\u70b9\u7684__init__.py\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<div class=\"line alt1\">\n<div class=\"cnblogs_code\">\n<pre>1 import pymysql\r\n2 pymysql.install_as_MySQLdb()<\/pre>\n<\/div>\n<p>\u505a\u5b8c\u4e0a\u8ff0\u52a8\u4f5c\u540e\uff0c\u5373\u53ef\u5728django\u4e2d\u8bbf\u95eemysql\u4e86\u3002<\/p>\n<\/div>\n<div id=\"digg\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u4e86\u4e00\u4e0bpython\u64cd\u4f5cmys&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,10],"tags":[],"class_list":["post-712","post","type-post","status-publish","format-standard","hentry","category-python","category-jishu"],"_links":{"self":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/712"}],"collection":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=712"}],"version-history":[{"count":1,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/712\/revisions"}],"predecessor-version":[{"id":990,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/712\/revisions\/990"}],"wp:attachment":[{"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=712"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}