{"id":745,"date":"2019-05-01T17:56:05","date_gmt":"2019-05-01T09:56:05","guid":{"rendered":"http:\/\/www.eait.co\/?p=745"},"modified":"2019-05-01T17:56:05","modified_gmt":"2019-05-01T09:56:05","slug":"python%e8%bf%9e%e6%8e%a5%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/notes.coremix.net\/?p=745","title":{"rendered":"python\u8fde\u63a5\u6570\u636e\u5e93"},"content":{"rendered":"<div>import pymysql<\/div>\n<div>db_config = {<\/div>\n<div>&#8216;host&#8217;:&#8217;127.0.0.1&#8242;, #\u5168\u90e8\u7528\u5b57\u7b26\u4e32<\/div>\n<div>&#8216;port&#8217;:3306, #\u6b64\u5904\u7528int<\/div>\n<div>&#8216;user&#8217;:&#8217;root&#8217;,<\/div>\n<div>&#8216;password&#8217;:&#8217;qwe123&#8242;,<\/div>\n<div>&#8216;db&#8217;:&#8217;moran&#8217;, #\u6570\u636e\u5e93\u540d\u5b57<\/div>\n<div>&#8216;charset&#8217;:&#8217;utf8&#8242;<\/div>\n<div>}<\/div>\n<div>conn = pymysql.connect(**db_config) #\u5efa\u7acb\u8fde\u63a5\u5bf9\u8c61<\/div>\n<div>cur = conn.cursor() #\u83b7\u53d6\u6e38\u6807\u5bf9\u8c61<\/div>\n<div># print(cur)<\/div>\n<div>sql = &#8216;select * from student&#8217;<\/div>\n<div>aa = cur.execute(sql)<\/div>\n<div>print(aa) #\u8fd4\u56de\u6267\u884c\u7684\u6570\u91cf<\/div>\n<div>print(cur.fetchall())<\/div>\n<div>aa = cur.execute(sql) #\u5982\u679c\u4e0d\u52a0\u8fd9\u4e2a,\u4e0b\u9762\u90a3\u884c\u6253\u5370\u51fa\u6765\u662f\u7a7a(\u6709\u70b9\u50cf\u8bfb\u53d6\u6587\u4ef6\u7684\u6307\u9488)<\/div>\n<div>print(cur.fetchmany(2))<\/div>\n<div>print(cur.fetchone())<\/div>\n<div>ccc = &#8216;insert into student(id,name,age) values(8,&#8221;namec&#8221;,19)&#8217;<\/div>\n<div>cur.execute(ccc)<\/div>\n<div>cur.execute(sql)<\/div>\n<div><a href=\"http:\/\/conn.commit\/\">conn.commit<\/a>()<\/div>\n<div>print(cur.fetchall())<\/div>\n<div><\/div>\n<div>#\u66f4\u65b0\u6570\u636e\u5e93<\/div>\n<div>ddd = &#8216;update student set id = -1,name=&#8221;new&#8221;,age = 66, sex =&#8221;nan&#8221; where id &gt;3&#8217;<\/div>\n<div>cur.execute(ddd)<\/div>\n<div><a href=\"http:\/\/conn.commit\/\">conn.commit<\/a>()<\/div>\n<div>cur.execute(&#8216;select * from student&#8217;)#\u5982\u679c\u4e0d\u52a0\u8fd9\u884c,\u6253\u5370\u7ed3\u679c\u4f1a\u6709(8, &#8216;namec&#8217;, 19, None)) \u9700\u8981\u91cd\u65b0\u67e5\u8be2\u91cd\u65b0\u6253\u5370<\/div>\n<div>print(cur.fetchall())<\/div>\n<div>#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/div>\n<div><\/div>\n<div>import redis<\/div>\n<div>aa = redis.StrictRedis(db = 5,host = &#8216;127.0.0.1&#8217;,decode_responses=True) #\u5728\u7a0b\u5e8f\u64cd\u4f5c\u7684\u6570\u636e\u4e3abyts\u7c7b\u578b,\u52a0\u5165decode_responses=True\u5199\u5165\u7684\u6570\u636e\u4e3astr\u7c7b\u578b,db\u4f7f\u7528\u7684\u6570\u636e\u5e93\u5e8f\u53f7\u4e0d\u5199\u4f7f\u75280\u53f7,\u540e\u9762\u7684\u7f16\u7801\u4e0d\u5199\u7684\u8bdd\u9ed8\u8ba4\u662f\u4e8c\u8fdb\u5236\u7c7b\u578b,\u5199\u7684\u8bdd\u9ed8\u8ba4\u662f\u5b57\u7b26\u4e32\u7c7b\u578b,\u7f16\u7801\u540e\u9762\u53ef\u4ee5\u518d\u52a0password<\/div>\n<div>print(aa.keys())<\/div>\n<div>print(aa.get(&#8216;k111&#8217;))<\/div>\n<div># aa.set(&#8216;ccc&#8217;,1111)<\/div>\n<div>aa.delete(&#8216;ccc&#8217;)<\/div>\n<div>aa.hset(&#8216;has_mr&#8217;,&#8217;name&#8217;,&#8217;a&#8217;)<\/div>\n<div>print(aa.hgetall(&#8216;has_mr&#8217;))<\/div>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>import pymysql db_config = { &#8216;host&#8217;:&#8217; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-745","post","type-post","status-publish","format-standard","hentry","category-python_note"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=745"}],"version-history":[{"count":1,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/745\/revisions"}],"predecessor-version":[{"id":746,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/745\/revisions\/746"}],"wp:attachment":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}