{"id":500,"date":"2017-02-02T21:46:24","date_gmt":"2017-02-02T13:46:24","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=500"},"modified":"2017-02-02T21:46:24","modified_gmt":"2017-02-02T13:46:24","slug":"python-article48","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/02\/python-article48\/","title":{"rendered":"Python\u8fde\u63a5mysql\u6570\u636e\u5e93\u5e76\u8fdb\u884c\u589e\u5220\u6539\u67e5\u64cd\u4f5c"},"content":{"rendered":"<h2>\u4eca\u5929\u5728\u4f7f\u7528Python\u8fde\u6570\u636e\u5e93\u7684\u65f6\u5019\u5fd8\u4e86\u600e\u4e48\u5199\u4e86\uff0c\u5230\u535a\u5ba2\u91cc\u770b\u4e86\u4e00\u4e0b\u53ea\u6709PHP\u548cJava\u7684\uff0c\u53ea\u597d\u53bb\u7b14\u8bb0\u91cc\u627e\u4e86\u4ee5\u524d\u7684\u8bb0\u5f55\uff0c\u5199\u5b8c\u4ee3\u7801\u5c31\u628a\u5173\u4e8ePython\u8fde\u6570\u636e\u5e93\u7684\u6587\u7ae0\u8865\u4e86\uff0c\u987a\u4fbf\u628a\u4e00\u4e9b\u5e38\u7528\u7684\u64cd\u4f5c\u5199\u51fa\u6765\uff0c\u65b9\u4fbf\u7528\u7684\u65f6\u5019\u770b\u4e00\u770b\u3002<\/h2>\n<h2>\u8bb0\u5f97\u6211\u521a\u4f7f\u7528Python\u8fde\u6570\u636e\u5e93\u7684\u65f6\u5019\u7814\u7a76\u5f88\u957f\u65f6\u95f4\uff0c\u6211\u7535\u8111\u91cc\u7684mysql\u662fphpstudy\u96c6\u6210\u7684\uff0c\u6211\u53c8\u5728\u865a\u62df\u673a\u91cc\u6d4b\u8bd5\uff0c\u6700\u540e\u624d\u53d1\u73b0\u4e0b\u9762\u662f\u53c8\u62a5\u9519\u4ee3\u53f7\u7684\uff0c\u4e3b\u673a\u540d\u51fa\u73b0\u9519\u8bef\u662f2003\uff0c\u800c\u6211\u4e00\u76f4\u5ffd\u7565\u4e86\u8fd9\u4e2a\u6700\u4e0b\u9762\u7684\u9519\u8bef\uff0c\u67e5\u4e86\u9519\u8bef\u4ee3\u53f7\u53d1\u73b0\u662flocalhost\u7684\u9519\uff0c\u5c06\u4e3b\u673a\u540d\u7531\u539f\u6765\u7684localhost\u66f4\u6539\u4e3a127.0.0.1\uff0c\u8fd9\u4e2a\u662f\u4e3b\u673a\u6d4b\u8bd5\u5730\u5740\uff0c\u5728\u4f7f\u7528Django\u7684\u65f6\u5019\u5c31\u51fa\u73b0\u8fc7\u8fd9\u4e2a\u6d4b\u8bd5\u5730\u5740\u3002\u5728PHP\u548cJava\u4e0a\u7528\u4e60\u60ef\u4e86\uff0c\u6211\u5728PHP\u4e0a\u8bd5\u4e86\u4e00\u4e0b127.0.0.1\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u90fd\u662f\u4e3b\u673a\u6d4b\u8bd5\u5730\u5740\uff0c\u53ef\u80fdPython\u6bd4\u8f83\u5947\u602a\u5427\uff0c\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\u518d\u8fde\u63a5\u5c31\u5f88\u7b80\u5355\u4e86\u3002<\/h2>\n<h2>\u6ce8\u610fconnect\u91cc\u7684charset=utf8\uff0c\u8fd9\u4e2a\u662f\u8981\u548c\u4f60\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\u5339\u914d\u7684\uff0c\u5982\u679c\u4e0d\u5199\u8fd9\u4e2a\uff0c\u67e5\u51fa\u6765\u7684\u4e2d\u6587\u6570\u636e\u5c31\u662f\u4e71\u7801\uff0c\u800c\u4e0d\u662f\u7f16\u7801\u5b57\u7b26\uff0c\u4e0b\u9762\u8fd8\u6709\u5e38\u7528\u7684\u4e00\u4e9b\u64cd\u4f5c\uff0c<\/h2>\n<pre><code>1.\u8fde\u63a5\u7684\u5efa\u7acb\u548c\u91ca\u653e\n-- coding=utf-8 --\nimport MySQLdb\nconn= MySQLdb.connect(\n        host='127.0.0.1',\n        port = 3306,\n        user='root',\n        passwd='root',\n        db ='manage',\n        charset='utf8',\n        )\ncur = conn.cursor()\n#\u521b\u5efa\u6570\u636e\u8868\n#cur.execute(\"create table student(id int ,name varchar(20),class varchar(30),age varchar(10))\")\n#\u63d2\u5165\u4e00\u6761\u6570\u636e\n#cur.execute(\"insert into student values('2','Tom','3 year 2 class','9')\")\n#\u4fee\u6539\u67e5\u8be2\u6761\u4ef6\u7684\u6570\u636e\n#cur.execute(\"update student set class='3 year 1 class' where name = 'Tom'\")\n#\u5220\u9664\u67e5\u8be2\u6761\u4ef6\u7684\u6570\u636e\n#cur.execute(\"delete from student where age='9'\")\ncur.close()\nconn.commit()\nconn.close()\n\u67e5\u8be2\u64cd\u4f5c\ncursor\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.\nrowcount \u8fd9\u662f\u4e00\u4e2a\u53ea\u8bfb\u5c5e\u6027,\u5e76\u8fd4\u56de\u6267\u884cexecute() \u65b9\u6cd5\u540e\u5f71\u54cd\u7684\u884c\u6570\u3002\nscroll(self, value, mode='relative') \u79fb\u52a8\u6307\u9488\u5230\u67d0\u4e00\u884c; \u5982\u679cmode='relative',\u5219\u8868\u793a\u4ece\u5f53\u524d\u6240\u5728\u884c\u79fb\u52a8value\u6761,\u5982\u679c mode='absolute',\u5219\u8868\u793a\u4ece\u7ed3\u679c\u96c6\u7684\u7b2c\u4e00\u884c\u79fb\u52a8value\u6761.\n#\u83b7\u5f97\u8868\u4e2d\u6709\u591a\u5c11\u6761\u6570\u636e\naa=cur.execute(\"select * from student\")\nprint aa\n#\u6253\u5370\u8868\u4e2d\u7684\u591a\u5c11\u6570\u636e\ninfo = cur.fetchmany(aa)\nfor ii in info:\n  print ii\n\u63d2\u5165\u64cd\u4f5c\ncursor\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\n#\u63d2\u5165\u4e00\u6761\u6570\u636e\nsqli=\"insert into student values(%s,%s,%s,%s)\"\ncur.execute(sqli,('3','Huhu','2 year 1 class','7'))\n#\u4e00\u6b21\u63d2\u5165\u591a\u6761\u8bb0\u5f55\nsqli=\"insert into student values(%s,%s,%s,%s)\"\ncur.executemany(sqli,[\n('3','Tom','1 year 1 class','6'),\n('3','Jack','2 year 1 class','7'),\n('3','Yaheng','2 year 2 class','7'),\n])\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5728\u4f7f\u7528Python\u8fde\u6570\u636e\u5e93\u7684\u65f6\u5019\u5fd8\u4e86\u600e\u4e48\u5199\u4e86\uff0c\u5230\u535a\u5ba2\u91cc\u770b\u4e86\u4e00\u4e0b\u53ea\u6709PHP\u548cJava\u7684\uff0c\u53ea\u597d\u53bb\u7b14\u8bb0\u91cc\u627e\u4e86\u4ee5\u524d\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/02\/python-article48\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":501,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[15],"tags":[58,75],"class_list":["post-500","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-python","tag-python","tag-database","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=500"}],"version-history":[{"count":0,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/500\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/501"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=500"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}