﻿{"id":478,"date":"2011-05-11T16:36:06","date_gmt":"2011-05-11T08:36:06","guid":{"rendered":"http:\/\/nick.workao.org\/?p=478"},"modified":"2011-05-11T21:33:58","modified_gmt":"2011-05-11T13:33:58","slug":"%e7%ae%80%e5%8d%95%e6%af%94%e8%be%83python%e7%9a%84%e6%95%b0%e6%8d%ae%e6%8c%81%e4%b9%85%e5%8c%96%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/python\/478","title":{"rendered":"MySQLdb for Python\u4f7f\u7528\u6307\u5357\/Python\u7684\u6570\u636e\u5e93\u64cd\u4f5c"},"content":{"rendered":"<p>\u7f51\u7ad9\u5c31\u662f\u8981\u548c\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92,\u5426\u5219\u4ec0\u4e48\u90fd\u4e0d\u7528\u505a\u4e86&#8230;\u4eca\u5929\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u53ebMySQLdb\u7684\u5e93,\u8fd9\u4e2a\u7528\u6765\u548cMySQL\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92.\n\u53ef\u4ee5\u4ece\u8fd9\u91cc\u83b7\u5f97\u8fd9\u4e2a\u5e93\n<a href=\"http:\/\/sourceforge.net\/projects\/mysql-python\" target=\"_blank\"><a href=\"http:\/\/sourceforge.net\/projects\/mysql-python\">http:\/\/sourceforge.net\/projects\/mysql-python<\/a><\/a>\n\u5982\u679c\u4f60\u4e0d\u786e\u5b9a\u4f60\u7684python\u73af\u5883\u91cc\u6709\u6ca1\u6709\u8fd9\u4e2a\u5e93,\u90a3\u5c31\u6253\u5f00python shell,\u8f93\u5165 import MySQLdb,\u5982\u679c\u8fd4\u56de\u9519\u8bef\u4fe1\u606f,\u90a3\u5c31\u8868\u793a\u4f60\u7684\u673a\u5668\u4e0a\u6ca1\u6709,\u8d76\u7d27\u53bb\u4e0b\u8f7d\u4e00\u4e2a.\u6211\u7684\u673a\u5668\u662fwin xp,\u6240\u4ee5\u6211\u4e0b\u8f7d\u4e86win\u73af\u5883\u4e0b\u7684exe\u90a3\u4e2a,\u76f4\u63a5\u53cc\u51fb\u5b8c\u6210\u5b89\u88c5.<\/p>\n<p>\u5728\u4ecb\u7ecd\u5177\u4f53\u7684\u64cd\u4f5c\u524d,\u5148\u82b1\u70b9\u65f6\u95f4\u6765\u8bf4\u8bf4\u4e00\u4e2a\u7a0b\u5e8f\u600e\u4e48\u548c\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\n1.\u548c\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5\n2.\u6267\u884csql\u8bed\u53e5,\u63a5\u6536\u8fd4\u56de\u503c\n3.\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\n\u4f7f\u7528MySQLdb\u4e5f\u8981\u9075\u5faa\u4e0a\u9762\u7684\u51e0\u6b65.\u8ba9\u6211\u4eec\u4e00\u6b65\u6b65\u7684\u8fdb\u884c.<\/p>\n<p><strong>0.\u5f15\u5165MySQLdb\u5e93<\/strong>\nimport MySQLdb<\/p>\n<p><strong>1.\u548c\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5<\/strong>\nconn=MySQLdb.connect(host=&#8221;localhost&#8221;,user=&#8221;root&#8221;,passwd=&#8221;sa&#8221;,db=&#8221;mytable&#8221;)\n\u63d0\u4f9b\u7684connect\u65b9\u6cd5\u7528\u6765\u548c\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5,\u63a5\u6536\u6570\u4e2a\u53c2\u6570,\u8fd4\u56de\u8fde\u63a5\u5bf9\u8c61.<\/p>\n<p>\u6bd4\u8f83\u5e38\u7528\u7684\u53c2\u6570\u5305\u62ec\nhost:\u6570\u636e\u5e93\u4e3b\u673a\u540d.\u9ed8\u8ba4\u662f\u7528\u672c\u5730\u4e3b\u673a.\nuser:\u6570\u636e\u5e93\u767b\u9646\u540d.\u9ed8\u8ba4\u662f\u5f53\u524d\u7528\u6237.\npasswd:\u6570\u636e\u5e93\u767b\u9646\u7684\u79d8\u5bc6.\u9ed8\u8ba4\u4e3a\u7a7a.\ndb:\u8981\u4f7f\u7528\u7684\u6570\u636e\u5e93\u540d.\u6ca1\u6709\u9ed8\u8ba4\u503c.\nport:MySQL\u670d\u52a1\u4f7f\u7528\u7684TCP\u7aef\u53e3.\u9ed8\u8ba4\u662f3306.\n\u66f4\u591a\u5173\u4e8e\u53c2\u6570\u7684\u4fe1\u606f\u53ef\u4ee5\u67e5\u8fd9\u91cc\n<a href=\"http:\/\/mysql-python.sourceforge.net\/MySQLdb.html\" target=\"_blank\"><a href=\"http:\/\/mysql-python.sourceforge.net\/MySQLdb.html\">http:\/\/mysql-python.sourceforge.net\/MySQLdb.html<\/a><\/a><\/p>\n<p>\u7136\u540e,\u8fd9\u4e2a\u8fde\u63a5\u5bf9\u8c61\u4e5f\u63d0\u4f9b\u4e86\u5bf9\u4e8b\u52a1\u64cd\u4f5c\u7684\u652f\u6301,\u6807\u51c6\u7684\u65b9\u6cd5\ncommit() \u63d0\u4ea4\nrollback() \u56de\u6eda<\/p>\n<p><strong>2.\u6267\u884csql\u8bed\u53e5\u548c\u63a5\u6536\u8fd4\u56de\u503c<\/strong>\ncursor=conn.cursor()\nn=cursor.execute(sql,param)\n\u9996\u5148,\u6211\u4eec\u7528\u4f7f\u7528\u8fde\u63a5\u5bf9\u8c61\u83b7\u5f97\u4e00\u4e2acursor\u5bf9\u8c61,\u63a5\u4e0b\u6765,\u6211\u4eec\u4f1a\u4f7f\u7528cursor\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u5de5\u4f5c.\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\u4e24\u5927\u7c7b:1.\u6267\u884c\u547d\u4ee4,2.\u63a5\u6536\u8fd4\u56de\u503c<\/p>\n<p>cursor\u7528\u6765\u6267\u884c\u547d\u4ee4\u7684\u65b9\u6cd5:\ncallproc(self, procname, args):\u7528\u6765\u6267\u884c\u5b58\u50a8\u8fc7\u7a0b,\u63a5\u6536\u7684\u53c2\u6570\u4e3a\u5b58\u50a8\u8fc7\u7a0b\u540d\u548c\u53c2\u6570\u5217\u8868,\u8fd4\u56de\u503c\u4e3a\u53d7\u5f71\u54cd\u7684\u884c\u6570\nexecute(self, query, args):\u6267\u884c\u5355\u6761sql\u8bed\u53e5,\u63a5\u6536\u7684\u53c2\u6570\u4e3asql\u8bed\u53e5\u672c\u8eab\u548c\u4f7f\u7528\u7684\u53c2\u6570\u5217\u8868,\u8fd4\u56de\u503c\u4e3a\u53d7\u5f71\u54cd\u7684\u884c\u6570\nexecutemany(self, query, args):\u6267\u884c\u5355\u6311sql\u8bed\u53e5,\u4f46\u662f\u91cd\u590d\u6267\u884c\u53c2\u6570\u5217\u8868\u91cc\u7684\u53c2\u6570,\u8fd4\u56de\u503c\u4e3a\u53d7\u5f71\u54cd\u7684\u884c\u6570\nnextset(self):\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u7ed3\u679c\u96c6<\/p>\n<p>cursor\u7528\u6765\u63a5\u6536\u8fd4\u56de\u503c\u7684\u65b9\u6cd5:\nfetchall(self):\u63a5\u6536\u5168\u90e8\u7684\u8fd4\u56de\u7ed3\u679c\u884c.\nfetchmany(self, size=None):\u63a5\u6536size\u6761\u8fd4\u56de\u7ed3\u679c\u884c.\u5982\u679csize\u7684\u503c\u5927\u4e8e\u8fd4\u56de\u7684\u7ed3\u679c\u884c\u7684\u6570\u91cf,\u5219\u4f1a\u8fd4\u56decursor.arraysize\u6761\u6570\u636e.\nfetchone(self):\u8fd4\u56de\u4e00\u6761\u7ed3\u679c\u884c.\nscroll(self, value, mode=&#8217;relative&#8217;):\u79fb\u52a8\u6307\u9488\u5230\u67d0\u4e00\u884c.\u5982\u679cmode=&#8217;relative&#8217;,\u5219\u8868\u793a\u4ece\u5f53\u524d\u6240\u5728\u884c\u79fb\u52a8value\u6761,\u5982\u679cmode=&#8217;absolute&#8217;,\u5219\u8868\u793a\u4ece\u7ed3\u679c\u96c6\u7684\u7b2c\u4e00\u884c\u79fb\u52a8value\u6761.<\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50.\n#\u4f7f\u7528sql\u8bed\u53e5,\u8fd9\u91cc\u8981\u63a5\u6536\u7684\u53c2\u6570\u90fd\u7528%s\u5360\u4f4d\u7b26.\u8981\u6ce8\u610f\u7684\u662f,\u65e0\u8bba\u4f60\u8981\u63d2\u5165\u7684\u6570\u636e\u662f\u4ec0\u4e48\u7c7b\u578b,\u5360\u4f4d\u7b26\u6c38\u8fdc\u90fd\u8981\u7528%s\nsql=&#8221;insert into cdinfo values(%s,%s,%s,%s,%s)&#8221;\n#param\u5e94\u8be5\u4e3atuple\u6216\u8005list\nparam=(title,singer,imgurl,url,alpha)\n#\u6267\u884c,\u5982\u679c\u6210\u529f,n\u7684\u503c\u4e3a1\nn=cursor.execute(sql,param)<\/p>\n<p>#\u518d\u6765\u6267\u884c\u4e00\u4e2a\u67e5\u8be2\u7684\u64cd\u4f5c\ncursor.execute(&#8220;select * from cdinfo&#8221;)\n#\u6211\u4eec\u4f7f\u7528\u4e86fetchall\u8fd9\u4e2a\u65b9\u6cd5.\u8fd9\u6837,cds\u91cc\u4fdd\u5b58\u7684\u5c06\u4f1a\u662f\u67e5\u8be2\u8fd4\u56de\u7684\u5168\u90e8\u7ed3\u679c.\u6bcf\u6761\u7ed3\u679c\u90fd\u662f\u4e00\u4e2atuple\u7c7b\u578b\u7684\u6570\u636e,\u8fd9\u4e9btuple\u7ec4\u6210\u4e86\u4e00\u4e2atuple\ncds=cursor.fetchall()\n#\u56e0\u4e3a\u662ftuple,\u6240\u4ee5\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\u7ed3\u679c\u96c6\nprint cds[0][3]\n#\u6216\u8005\u76f4\u63a5\u663e\u793a\u51fa\u6765,\u770b\u770b\u7ed3\u679c\u96c6\u7684\u771f\u5b9e\u6837\u5b50\nprint cds<\/p>\n<p>#\u5982\u679c\u9700\u8981\u6279\u91cf\u7684\u63d2\u5165\u6570\u636e,\u5c31\u8fd9\u6837\u505a\nsql=&#8221;insert into cdinfo values(0,%s,%s,%s,%s,%s)&#8221;\n#\u6bcf\u4e2a\u503c\u7684\u96c6\u5408\u4e3a\u4e00\u4e2atuple,\u6574\u4e2a\u53c2\u6570\u96c6\u7ec4\u6210\u4e00\u4e2atuple,\u6216\u8005list\nparam=((title,singer,imgurl,url,alpha),(title2,singer2,imgurl2,url2,alpha2))\n#\u4f7f\u7528executemany\u65b9\u6cd5\u6765\u6279\u91cf\u7684\u63d2\u5165\u6570\u636e.\u8fd9\u771f\u662f\u4e00\u4e2a\u5f88\u9177\u7684\u65b9\u6cd5!\nn=cursor.executemany(sql,param)<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f(\u6216\u8005\u8bf4\u662f\u6211\u611f\u5230\u5947\u602a\u7684\u662f),\u5728\u6267\u884c\u5b8c\u63d2\u5165\u6216\u5220\u9664\u6216\u4fee\u6539\u64cd\u4f5c\u540e,\u9700\u8981\u8c03\u7528\u4e00\u4e0bconn.commit()\u65b9\u6cd5\u8fdb\u884c\u63d0\u4ea4.\u8fd9\u6837,\u6570\u636e\u624d\u4f1a\u771f\u6b63\u4fdd\u5b58\u5728\u6570\u636e\u5e93\u4e2d.\u6211\u4e0d\u6e05\u695a\u662f\u5426\u662f\u6211\u7684mysql\u8bbe\u7f6e\u95ee\u9898,\u603b\u4e4b,\u4eca\u5929\u6211\u5728\u4e00\u5f00\u59cb\u4f7f\u7528\u7684\u65f6\u5019,\u5982\u679c\u4e0d\u7528commit,\u90a3\u6570\u636e\u5c31\u4e0d\u4f1a\u4fdd\u7559\u5728\u6570\u636e\u5e93\u4e2d,\u4f46\u662f,\u6570\u636e\u786e\u5b9e\u5728\u6570\u636e\u5e93\u5446\u8fc7.\u56e0\u4e3a\u81ea\u52a8\u7f16\u53f7\u8fdb\u884c\u4e86\u7d2f\u79ef,\u800c\u4e14\u8fd4\u56de\u7684\u53d7\u5f71\u54cd\u7684\u884c\u6570\u5e76\u4e0d\u4e3a0.<\/p>\n<p><strong>3.\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong>\n\u9700\u8981\u5206\u522b\u7684\u5173\u95ed\u6307\u9488\u5bf9\u8c61\u548c\u8fde\u63a5\u5bf9\u8c61.\u4ed6\u4eec\u6709\u540d\u5b57\u76f8\u540c\u7684\u65b9\u6cd5\ncursor.close()\nconn.close()<\/p>\n<p>\u4e09\u6b65\u5b8c\u6210,\u57fa\u672c\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u5c31\u662f\u8fd9\u6837\u4e86.\u4e0b\u9762\u662f\u4e24\u4e2a\u6709\u7528\u7684\u8fde\u63a5\nMySQLdb\u7528\u6237\u6307\u5357: <a href=\"http:\/\/mysql-python.sourceforge.net\/MySQLdb.html\" target=\"_blank\"><a href=\"http:\/\/mysql-python.sourceforge.net\/MySQLdb.html\">http:\/\/mysql-python.sourceforge.net\/MySQLdb.html<\/a><\/a>\nMySQLdb\u6587\u6863: <a href=\"http:\/\/mysql-python.sourceforge.net\/MySQLdb-1.2.2\/public\/MySQLdb-module.html\" target=\"_blank\"><a href=\"http:\/\/mysql-python.sourceforge\">http:\/\/mysql-python.sourceforge<\/a>. &#8230; MySQLdb-module.html<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u7f51\u7ad9\u5c31\u662f\u8981\u548c\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92,\u5426\u5219\u4ec0\u4e48\u90fd\u4e0d\u7528\u505a\u4e86&#8230;\u4eca\u5929\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u53ebMySQLdb\u7684\u5e93,\u8fd9\u4e2a\u7528\u6765\u548cMySQL\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92. \u53ef\u4ee5\u4ece\u8fd9\u91cc\u83b7\u5f97\u8fd9\u4e2a\u5e93 <a href=\"http:\/\/sourceforge.ne&#46;&#46;&#038;#46\">http:\/\/sourceforge.ne&#46;&#46;&#038;#46<\/a>;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[274],"tags":[100,284,275],"class_list":["post-478","post","type-post","status-publish","format-standard","hentry","category-python","tag-mysql","tag-mysqldb","tag-python-2"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=478"}],"version-history":[{"count":3,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":481,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/478\/revisions\/481"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}