{"id":207826,"date":"2025-07-08T14:26:31","date_gmt":"2025-07-08T06:26:31","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207826\/"},"modified":"2025-07-08T14:26:31","modified_gmt":"2025-07-08T06:26:31","slug":"%e4%bb%8e-mysql-%e7%bb%93%e6%9e%9c%e7%94%9f%e6%88%90-csv-%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207826\/","title":{"rendered":"\u4ece MySQL \u7ed3\u679c\u751f\u6210 .CSV \u6587\u4ef6"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4ece MySQL \u7ed3\u679c\u751f\u6210 .CSV \u6587\u4ef6<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 21:36:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u76ee\u524d\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4ece MySQL \u7ed3\u679c\u751f\u6210 .CSV \u6587\u4ef6\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u4f7f\u7528 go \u751f\u6210\u4e00\u4e2a csv \u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u5c06\u5b58\u50a8 mysql \u67e5\u8be2\u7684\u8f6c\u50a8\u3002<\/p>\n<p>\u6211\u76ee\u524d\u53ef\u4ee5\u5c06\u7ed3\u679c\u5bfc\u51fa\u5230\u9884\u5148\u5b58\u5728\u7684 csv \u6587\u4ef6\uff0c\u4f46\u6211\u5c1d\u8bd5\u5728 main.go \u8fd0\u884c\u540e\u81ea\u52a8\u751f\u6210 csv \u6587\u4ef6\u3002\u6211\u5c1d\u8bd5\u4f7f\u7528 <code>writefile<\/code>\uff0c\u6211\u77e5\u9053\u5b83\u4f1a\u5c06 csv \u6587\u4ef6\u5199\u5165\u6307\u5b9a\u7684\u6587\u4ef6\u540d\u3002\u6211\u77e5\u9053\u8fd9\u662f\u8bbe\u8ba1\u4f7f\u7136\uff0c\u4f46\u6211\u5e0c\u671b\u751f\u6210\u8be5\u6587\u4ef6\u3002<\/p>\n<pre>rows, _ := db.Query(\"SELECT * FROM orderTest limit 100;\")\n\n    err := sqltocsv.WriteFile(\"orderTest.csv\", rows)\n    if err != nil {\n        panic(err)\n    }\n\n    columns, _ := rows.Columns()\n    count := len(columns)\n    values := make([]interface{}, count)\n    valuePtrs := make([]interface{}, count)\n\n    for rows.Next() {\n        for i := range columns {\n            valuePtrs[i] = &amp;values[i]\n        }\n\n        rows.Scan(valuePtrs...)\n\n        for i, col := range columns {\n            val := values[i]\n\n            b, ok := val.([]byte)\n            var v interface{}\n            if ok {\n                v = string(b)\n            } else {\n                v = val\n            }\n\n            fmt.Println(col, v)\n        }\n    }\n}<\/pre>\n<p>\u6211\u7684\u76ee\u6807\u662f\u8ba9 ordetest.csv \u6587\u4ef6\u5728\u8fd0\u884c main.go \u65f6\u81ea\u52a8\u521b\u5efa<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p><code>sqltocsv.writefile(...)<\/code> \u5e94\u8be5\u4e3a\u60a8\u521b\u5efa\u8be5\u6587\u4ef6\uff08\u5982\u679c\u8be5\u6587\u4ef6\u4e0d\u5b58\u5728\uff09\u3002<\/p>\n<p>\u5728\u5e95\u5c42\uff0c\u5b83\u53ea\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684 <code>os.create(...)<\/code>\u3002<\/p>\n<p>github.com\/joho\/sqltocsv\/sqltocsv.go\uff1a<\/p>\n<pre>\/\/ writefile writes the csv to the filename specified, return an error if problem\nfunc (c converter) writefile(csvfilename string) error {\n    f, err := os.create(csvfilename)\n    if err != nil {\n        return err\n    }\n\n    err = c.write(f)\n    if err != nil {\n        f.close() \/\/ close, but only return\/handle the write error\n        return err\n    }\n\n    return f.close()\n}<\/pre>\n<p><code>os.create(...)<\/code> \u7684\u6587\u6863\uff1a<\/p>\n<pre>\/\/ Create creates the named file with mode 0666 (before umask), truncating\n\/\/ it if it already exists. If successful, methods on the returned\n\/\/ File can be used for I\/O; the associated file descriptor has mode\n\/\/ O_RDWR.\n\/\/ If there is an error, it will be of type *PathError.\nfunc Create(name string) (*File, error) {\n    return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)\n}<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u4ece MySQL \u7ed3\u679c\u751f\u6210 .CSV \u6587\u4ef6\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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":[4925],"tags":[],"class_list":["post-207826","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207826","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=207826"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207826\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}