{"id":200428,"date":"2025-05-06T10:00:08","date_gmt":"2025-05-06T02:00:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200428\/"},"modified":"2025-05-06T10:00:08","modified_gmt":"2025-05-06T02:00:08","slug":"%e5%be%aa%e7%8e%af%e8%af%bb%e5%8f%96-excel-%e5%b9%b6%e5%86%99%e5%85%a5-mysql%ef%bc%8c%e5%a6%82%e4%bd%95%e4%bc%98%e5%8c%96%e6%80%a7%e8%83%bd%e4%bb%a5%e9%81%bf%e5%85%8d%e9%80%9f%e5%ba%a6%e5%8f%98","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200428\/","title":{"rendered":"\u5faa\u73af\u8bfb\u53d6 Excel \u5e76\u5199\u5165 MySQL\uff0c\u5982\u4f55\u4f18\u5316\u6027\u80fd\u4ee5\u907f\u514d\u901f\u5ea6\u53d8\u6162\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5faa\u73af\u8bfb\u53d6 Excel \u5e76\u5199\u5165 MySQL\uff0c\u5982\u4f55\u4f18\u5316\u6027\u80fd\u4ee5\u907f\u514d\u901f\u5ea6\u53d8\u6162\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241105\/1730765730672963a2288d9.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5faa\u73af\u8bfb\u53d6excel \u5e76\u5199\u5165mysql \u7684\u6027\u80fd\u4f18\u5316<\/strong><\/p>\n<p><strong>\u80cc\u666f\uff1a<\/strong><\/p>\n<p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u5faa\u73af\u8bfb\u53d6excel\u6587\u4ef6\u5e76\u5c06\u5176\u5199\u5165mysql \u6570\u636e\u5e93\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4f46\u662f\uff0c\u968f\u7740\u6587\u4ef6\u6570\u91cf\u7684\u589e\u52a0\uff0c\u63d2\u5165\u901f\u5ea6\u53ef\u80fd\u4f1a\u663e\u7740\u964d\u4f4e\u3002 <\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 for \u5faa\u73af\u6253\u5f00\u591a\u4e2a excel \u6587\u4ef6\u5e76\u5199\u5165\u6570\u636e\u5e93\u65f6\uff0c\u521d\u59cb\u6267\u884c\u901f\u5ea6\u8f83\u5feb\uff0c\u4f46\u968f\u540e\u901f\u5ea6\u4f1a\u8d8a\u6765\u8d8a\u6162\u3002 <\/p>\n<p><strong>\u5206\u6790\uff1a<\/strong><\/p>\n<p>\u6839\u636e\u95ee\u9898\u63cf\u8ff0\uff0c\u53ef\u4ee5\u63a8\u6d4b\u7f13\u6162\u7684\u6839\u6e90\u53ef\u80fd\u662f\u4ee5\u4e0b\u539f\u56e0\u4e4b\u4e00\uff1a<\/p>\n<ul>\n<li><strong>\u6570\u636e\u5e93\u8fde\u63a5\u5ef6\u8fdf\uff1a<\/strong>\u6bcf\u6b21\u5faa\u73af\u6253\u5f00\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\u53ef\u80fd\u4f1a\u5bfc\u81f4\u65f6\u95f4\u5f00\u9500\u3002 <\/li>\n<li><strong>\u6253\u5370\u8f93\u51fa\uff1a<\/strong>\u6253\u5370\u6570\u636e\u5230\u63a7\u5236\u53f0\u6216\u65e5\u5fd7\u6587\u4ef6\u4f1a\u51cf\u6162\u7a0b\u5e8f\u6267\u884c\u901f\u5ea6\u3002 <\/li>\n<li><strong>\u6587\u4ef6\u5904\u7406\u5f00\u9500\uff1a<\/strong>\u53cd\u590d\u52a0\u8f7d\u548c\u5173\u95ed excel \u6587\u4ef6\u4e5f\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002 <\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p><strong>1. \u4f18\u5316\u6570\u636e\u5e93\u8fde\u63a5\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668( with \u8bed\u53e5) \u6765\u7ba1\u7406\u8fde\u63a5\uff0c\u4ece\u800c\u907f\u514d\u6bcf\u6b21\u5faa\u73af\u90fd\u5efa\u7acb\u548c\u5173\u95ed\u8fde\u63a5\u3002 <\/li>\n<li>\u8003\u8651\u4f7f\u7528\u8fde\u63a5\u6c60\u6765\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u4ee5\u51cf\u5c11\u5efa\u7acb\u548c\u5173\u95ed\u8fde\u63a5\u7684\u5f00\u9500\u3002 <\/li>\n<\/ul>\n<p><strong>2. \u7981\u7528\u6253\u5370\u8f93\u200b\u200b\u51fa\uff1a<\/strong><\/p>\n<ul>\n<li>\u6ce8\u91ca\u6389\u6240\u6709\u8c03\u8bd5\u6216\u65e5\u5fd7\u6253\u5370\u8bed\u53e5\uff0c\u4ee5\u4fbf\u4ec5\u6267\u884c\u5fc5\u8981\u7684\u64cd\u4f5c\u3002 <\/li>\n<\/ul>\n<p><strong>3. \u4f18\u5316\u6587\u4ef6\u5904\u7406\uff1a<\/strong><\/p>\n<ul>\n<li>\u907f\u514d\u591a\u6b21\u52a0\u8f7d\u548c\u5173\u95ed excel \u6587\u4ef6\u3002\u4f7f\u7528\u5355\u4e2a load_workbook() \u5b9e\u4f8b\u6765\u52a0\u8f7d\u6240\u6709\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 ws.iter_rows() \u65b9\u6cd5\u8fed\u4ee3\u884c\uff0c\u800c\u4e0d\u662f\u8bfb\u53d6\u6bcf\u4e2a\u5355\u72ec\u7684\u5355\u5143\u683c\u3002 <\/li>\n<\/ul>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>from openpyxl import load_workbook\nimport pymysql\n\n# \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\ndb = pymysql.connect(host=\"localhost\", port=3306, user=\"test\",\n                     password=\"123456\", database=\"test\", charset=\"utf8\")\ncur = db.cursor()\n\nwb = load_workbook(r'E:\\\u6574\u7406\u6570\u636e\\4\u6708\\1 (17).xlsx')\nws = wb.active\n\n# \u5b9a\u4e49\u9700\u8981\u63d2\u5165\u7684\u5b57\u6bb5\nfields = [\"d\", \"fb\", \"kh\", \"wdzl\", \"zxzl\", \"jpzl\", \"jszl\", \"jpinfo\", \"ywtime\"]\n\n# \u904d\u5386\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u884c\nfor row in ws.iter_rows(min_row=2):\n    values = [cell.value for cell in row]\n\n    # \u6784\u5efa\u63d2\u5165\u8bed\u53e5\n    sql = \"INSERT INTO testdata ({}) VALUES ({})\".format(\n        \",\".join(fields), \",\".join([\"%s\"] * len(fields))\n    )\n\n    # \u6267\u884c\u63d2\u5165\u64cd\u4f5c\n    cur.execute(sql, values)\n    db.commit()\n\n# \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\ncur.close()\ndb.close()<\/pre>\n<p><strong>\u5176\u4ed6\u5efa\u8bae\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u6570\u636e\u5e93\u7d22\u5f15\u5df2\u5efa\u7acb\uff0c\u4ee5\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u3002 <\/li>\n<li>\u6839\u636e\u9700\u8981\u8c03\u6574 mysql \u914d\u7f6e\uff0c\u4f8b\u5982\u589e\u52a0 innodb_buffer_pool_size\u3002 <\/li>\n<li>\u8003\u8651\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5e76\u884c\u5904\u7406\u6587\u4ef6\u3002 <\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5faa\u73af\u8bfb\u53d6 Excel \u5e76\u5199\u5165 MySQL\uff0c\u5982\u4f55\u4f18\u5316\u6027\u80fd\u4ee5\u907f\u514d\u901f\u5ea6\u53d8\u6162\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6570\u636e\u5e93\u77e5\u8bc6\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5faa\u73af\u8bfb\u53d6 Excel \u5e76\u5199\u5165 M&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200428","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200428"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200428\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}