{"id":6442,"date":"2014-07-17T17:19:28","date_gmt":"2014-07-17T09:19:28","guid":{"rendered":"http:\/\/jpuyy.com\/?p=6442"},"modified":"2014-07-17T17:19:28","modified_gmt":"2014-07-17T09:19:28","slug":"python-mysqldb","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=6442","title":{"rendered":"python\u64cd\u4f5cmysql"},"content":{"rendered":"<p>yum\u5b89\u88c5<\/p>\n<pre>yum install MySQL-python<\/pre>\n<p>\u4f7f\u7528\u6b64\u6a21\u5757<\/p>\n<pre>import MySQLdb #\u6ce8\u610f\u5927\u5c0f\u5199\uff01\uff01<\/pre>\n<p>#\u5efa\u7acb\u548c\u6570\u636e\u5e93\u7cfb\u7edf\u7684\u8fde\u63a5<\/p>\n<pre> conn = MySQLdb.connect(host='localhost', user='root',passwd='root')<\/pre>\n<p>#\u83b7\u53d6\u64cd\u4f5c\u6e38\u6807<\/p>\n<pre> cursor = conn.cursor()<\/pre>\n<p>#\u9009\u62e9\u6570\u636e\u5e93<\/p>\n<pre> conn.select_db('python')<\/pre>\n<p>#\u6267\u884cSQL,\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93.<\/p>\n<pre> cursor.execute(\"\"\"select * from\u00a0test\"\"\")<\/pre>\n<p>#\u83b7\u53d6\u4e00\u6761\u8bb0\u5f55,\u6bcf\u6761\u8bb0\u5f55\u505a\u4e3a\u4e00\u4e2a\u5143\u7ec4\u8fd4\u56de<\/p>\n<pre> result = cursor.fetchone();\r\n print result\r\n #print 'ID: %s info: %s' % (result[0],result[1])\r\n print 'ID: %s info: %s' % result<\/pre>\n<p>#\u83b7\u53d65\u6761\u8bb0\u5f55\uff0c\u6ce8\u610f\u7531\u4e8e\u4e4b\u524d\u6267\u884c\u6709\u4e86fetchone()\uff0c\u6240\u4ee5\u6e38\u6807\u5df2\u7ecf\u6307\u5230\u7b2c\u4e8c\u6761\u8bb0\u5f55\u4e86\uff0c\u4e5f\u5c31\u662f\u4ece\u7b2c\u4e8c\u6761\u5f00\u59cb\u7684\u6240\u6709\u8bb0\u5f55<\/p>\n<pre> print \"\u53ea\u83b7\u53d65\u6761\u8bb0\u5f55:\"\r\n results = cursor.fetchmany(5)\r\n for r in results:\r\n print r<\/pre>\n<p>print &#8220;\u83b7\u53d6\u6240\u6709\u7ed3\u679c:&#8221;<br \/>\n#\u91cd\u7f6e\u6e38\u6807\u4f4d\u7f6e\uff0c0,\u4e3a\u504f\u79fb\u91cf\uff0cmode\uff1dabsolute | relative,\u9ed8\u8ba4\u4e3arelative,<\/p>\n<pre> cursor.scroll(0,mode='absolute')\r\n #\u83b7\u53d6\u6240\u6709\u7ed3\u679c\r\n results = cursor.fetchall()\r\n for r in results:\r\n print r\r\n conn.close()<\/pre>\n<p>#\u5173\u95ed\u8fde\u63a5\uff0c\u91ca\u653e\u8d44\u6e90<\/p>\n<pre> cursor.close();<\/pre>\n<p>http:\/\/www.iteye.com\/topic\/573092<\/p>\n","protected":false},"excerpt":{"rendered":"<p>yum\u5b89\u88c5 yum install MySQL-python \u4f7f\u7528\u6b64\u6a21\u5757 import MySQLdb #\u6ce8\u610f\u5927\u5c0f\u5199\uff01\uff01 #\u5efa\u7acb\u548c\u6570\u636e\u5e93\u7cfb\u7edf\u7684\u8fde\u63a5 conn = MySQLdb.connect(host=&#8217;localhost&#8217;, user=&#8217;root&#8217;,passwd=&#8217;root&#8217;) #\u83b7\u53d6\u64cd\u4f5c\u6e38\u6807 cursor = conn.cursor() #\u9009\u62e9\u6570\u636e\u5e93 conn.select_db(&#8216;python&#8217;) #\u6267\u884cSQL,\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93. cursor.execute(&#8220;&#8221;&#8221;select * from\u00a0test&#8221;&#8221;&#8221;) #\u83b7\u53d6\u4e00\u6761\u8bb0\u5f55,\u6bcf\u6761\u8bb0\u5f55\u505a\u4e3a\u4e00\u4e2a\u5143\u7ec4\u8fd4\u56de result = cursor.fetchone(); print result #print &#8216;ID: %s info: %s&#8217; % (result[0],result[1]) print &#8216;ID: %s info: %s&#8217; % result #\u83b7\u53d65\u6761\u8bb0\u5f55\uff0c\u6ce8\u610f\u7531\u4e8e\u4e4b\u524d\u6267\u884c\u6709\u4e86fetchone()\uff0c\u6240\u4ee5\u6e38\u6807\u5df2\u7ecf\u6307\u5230\u7b2c\u4e8c\u6761\u8bb0\u5f55\u4e86\uff0c\u4e5f\u5c31\u662f\u4ece\u7b2c\u4e8c\u6761\u5f00\u59cb\u7684\u6240\u6709\u8bb0\u5f55 print &#8220;\u53ea\u83b7\u53d65\u6761\u8bb0\u5f55:&#8221; results = cursor.fetchmany(5) for r in results: print r [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-6442","post","type-post","status-publish","format-standard","hentry","category-life"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6442"}],"version-history":[{"count":8,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6442\/revisions"}],"predecessor-version":[{"id":6482,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6442\/revisions\/6482"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}