{"id":87577,"date":"2020-05-10T13:05:02","date_gmt":"2020-05-10T05:05:02","guid":{"rendered":"http:\/\/4563.org\/?p=87577"},"modified":"2020-05-10T13:05:02","modified_gmt":"2020-05-10T05:05:02","slug":"%e8%af%b7%e6%95%99%e4%b8%aa%e7%88%ac-%e8%99%ab%e5%ad%98-mysql-%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=87577","title":{"rendered":"\u8bf7\u6559\u4e2a\u722c \u866b\u5b58 mysql \u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e2a\u722c \u866b\u5b58 mysql \u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Colorful <\/span>  <span><i><\/i> 19<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>import pymongo<\/h1>\n<p> <\/p>\n<h1>from pymongo import MongoClient<\/h1>\n<p> <\/p>\n<p>import pymysql as pq<\/p>\n<p>class Pipeline(object): def <strong>init<\/strong>(self): self.conn = pq.connect(host=&#8217;127.0.0.1&#8242;, user=&#8217;root&#8217;,port=&#8217;3306&#8242;, passwd=&#8217;123456&#8242;, db=&#8217;root&#8217;, charset=&#8217;utf8&#8242;) self.cur = self.conn.cursor() # def <strong>init<\/strong>(self): # host = &#8216;127.0.0.1&#8217; # port = 27017 # client = pymongo.MongoClient(host=host,port=port) # collection = client.local.letsgojpt # self.post = collection<\/p>\n<pre><code>def process_item(self, item, spider):     for i in range(0,len(item[\"title_all\"])):         # sql = \"insert into letsgojp(title_all, center_all) VALUES (%s, %s)\"         self.cur.execute(\"insert into letsgojp(title_all,center_all) values(%s,%s)\",(item['title_all'][i],item['center_all'][i]))         self.conn.commit()         # self.post.insert(data)     return item  def close_spider(self, spider):     self.cur.close()     self.conn.close() <\/code><\/pre>\n<p>\u4e3b\u8981\u662f\u8fd9\u884c\u62a5\u9519<\/p>\n<p>self.cur.execute(&#8220;insert into letsgojp(title_all,center_all) values(%s,%s)&#8221;,(item[&#8216;title_all&#8217;][i],item[&#8216;center_all&#8217;][i]))<\/p>\n<p>\u7528 mongo \u7684\u65f6\u5019\u662f\u6b63\u5e38\u5b58\u6570\u636e\u7684\uff0c\u4f46\u662f\u7528 mysql \u5c31\u62a5\u9519\uff0c\u7f51\u4e0a\u627e\u4e00\u4e9b\uff0c\u4e0d\u592a\u4f1a\u7528\uff0c\u8bf7\u6559\u5404\u4f4d\u5927\u795e\u4e86<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>14<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1453636\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. \u627e\u4e2a\u8d34\u4ee3\u7801\u7684\u5730\u65b9\u628a\u7f29\u8fdb\u7559\u4e0b&#8230;<\/p>\n<p>2. \u62a5\u9519\u4fe1\u606f\u89e3\u91ca 90% \u95ee\u9898, \u5927\u90e8\u5206\u65f6\u95f4\u62a5\u9519\u4fe1\u606f\u6700\u540e\u4e00\u884c\u4e00\u641c stackoverflow \u4e0a\u90fd\u6709\u540c\u884c\u9047\u5230\u8fc7<\/p>\n<p>3. \u7eaf\u770b\u7684\u8bdd, \u5b9e\u5728\u731c\u4e0d\u5230\u554a&#8230; \u5c31\u8bed\u6cd5\u6765\u8bf4, pymysql \u4f1a\u81ea\u52a8\u8f6c\u4e49, \u95ee\u9898\u4e0d\u5927, \u770b\u4f60\u7684\u5217\u540d\u4e5f\u6ca1\u6709\u654f\u611f\u5173\u952e\u5b57\u6240\u4ee5\u4e0d\u52a0\u53cd\u5f15\u53f7\u4e5f\u6ca1\u4e8b                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453637\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jugelizi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             [item[&#8216;title_all&#8217;][i],item[&#8216;center_all&#8217;][i]] \u662f\u4e0d\u662f\uff1f<br \/>\u4e5f\u6709\u53ef\u80fd\u6ca1\u5904\u7406\u597d\u7279\u6b8a\u5b57\u7b26 \u9519\u8bef\u63d0\u793a\u8d34\u4e0b\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453638\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Colorful <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jugelizi \u5e94\u8be5\u4e0d\u4f1a\u7684\u5427\uff0c\u56e0\u4e3a\u5b58 mongo \u7684\u65f6\u5019\u662f\u6ca1\u603b\u662f\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453639\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chaneyccy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u8d34\u62a5\u9519\u4fe1\u606f\uff0c\u8fd9\u4e5f\u770b\u4e0d\u51fa\u6765\u54ea\u91cc\u6709\u95ee\u9898\u5427<\/p>\n<p>\u6ca1\u7528\u8fc7 SQL \u597d\u5947\u641c\u4e86\u4e00\u5708\uff0c\u6ca1\u6709\u53d1\u73b0\u6a21\u5757\u5bfc\u5165\u662f import pymysql as pq\uff0c\u95ee\u9898\u5728\u8fd9\uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453640\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Colorful <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @chaneyccy \u4e0d\u662f\u90a3\u91cc\uff0c\u80fd\u722c\u5230\u4e00\u6761\u6570\u636e <br \/>self.cur.execute(&#8220;insert into letsgojp(title_all,center_all) values(%s,%s)&#8221;,(item[&#8216;title_all&#8217;][i],item[&#8216;center_all&#8217;][i]))<\/p>\n<p>\u4e3b\u8981\u5c31\u662f\u8fd9\u884c                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453641\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : aydd2004 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             execute \u7684\u5185\u5bb9 print \u51fa\u6765\u770b\u4e00\u4e0b <\/p>\n<p>\u5b9e\u5728\u4e0d\u884c\u7c98\u5230 mysql \u91cc\u9762\u6267\u884c\u4e00\u4e0b                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453642\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dorothyREN <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5360\u4f4d\u7b26\u90a3\u91cc\u7684\u95ee\u9898\uff0c\u63d2\u5165\u5b57\u7b26\u4e32 \u9700\u8981 \u52a0\u4e0a &#8216;%s&#8217;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453643\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dorothyREN <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @dorothyREN #7 &#8220;` def save_data(self, title, content):<br \/> sql = &#8220;INSERT INTO test(title,content) VALUES (&#8216;%s&#8217;,&#8217;%s&#8217;)&#8221; % (title, content)<br \/> # print(sql)<br \/> self.cur.execute(sql)<br \/> self.conn.commit()<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453645\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : crella <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             mysql \u65b0\u7248\u7684 mb-utf8 \u7f16\u7801\u8bbe\u7f6e\u6709\u70b9\u9ebb\u70e6\u3002<\/p>\n<p>\u5efa\u8bae\u76f4\u63a5 postgresql                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453646\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : triangle111 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             print \u4e00\u4e0b sql \u8bed\u53e5\u5230 mysql \u6267\u884c\u5c31\u77e5\u9053\u9519\u54ea\u4e86\uff0c \u5e94\u8be5\u662f\u5b57\u7b26\u4e32\u6ca1\u6709\u5f15\u53f7\u5bfc\u81f4\u7684\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453647\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : annielong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e00\u822c\u90fd\u662f\u7f16\u7801\u548c\u975e\u6cd5\u5b57\u7b26\u7684\u95ee\u9898\uff0c\u6253\u5370 sql \u8bed\u53e5\u5c31\u53ef\u4ee5\u770b\u51fa\u6765                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453648\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhangysh1995 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6dfb\u52a0 try except\uff0c\u6293 mysql.connector.errors.DatabaseError \u9519\u8bef                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453650\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leapV3 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8bd5\u8bd5\u5c06\u8868\u540d\u4e0e\u5217\u540d\u52a0\u4e0a\u53cd\u5f15\u53f7                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1453652\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : AmberJiang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u50cf\u662f insert \u8bed\u53e5\u90a3\u91cc\u5b58\u5728\u8bed\u6cd5\u9519\u8bef\u3002\u3002\u3002\u8d34\u62a5\u9519\u56fe\u51fa\u6765\u770b\u770b\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e2a\u722c \u866b\u5b58 mysql \u7684\u95ee\u9898&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/87577"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=87577"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/87577\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=87577"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=87577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}