<bdo id='Cd8wi'></bdo><ul id='Cd8wi'></ul>
  • <small id='Cd8wi'></small><noframes id='Cd8wi'>

    <tfoot id='Cd8wi'></tfoot>
    <i id='Cd8wi'><tr id='Cd8wi'><dt id='Cd8wi'><q id='Cd8wi'><span id='Cd8wi'><b id='Cd8wi'><form id='Cd8wi'><ins id='Cd8wi'></ins><ul id='Cd8wi'></ul><sub id='Cd8wi'></sub></form><legend id='Cd8wi'></legend><bdo id='Cd8wi'><pre id='Cd8wi'><center id='Cd8wi'></center></pre></bdo></b><th id='Cd8wi'></th></span></q></dt></tr></i><div id='Cd8wi'><tfoot id='Cd8wi'></tfoot><dl id='Cd8wi'><fieldset id='Cd8wi'></fieldset></dl></div>

      1. <legend id='Cd8wi'><style id='Cd8wi'><dir id='Cd8wi'><q id='Cd8wi'></q></dir></style></legend>

        如何将 CSV 文件导入 MySQL 表?

        时间:2023-08-20
        <legend id='1IkFE'><style id='1IkFE'><dir id='1IkFE'><q id='1IkFE'></q></dir></style></legend>

        <small id='1IkFE'></small><noframes id='1IkFE'>

            <tbody id='1IkFE'></tbody>
          1. <i id='1IkFE'><tr id='1IkFE'><dt id='1IkFE'><q id='1IkFE'><span id='1IkFE'><b id='1IkFE'><form id='1IkFE'><ins id='1IkFE'></ins><ul id='1IkFE'></ul><sub id='1IkFE'></sub></form><legend id='1IkFE'></legend><bdo id='1IkFE'><pre id='1IkFE'><center id='1IkFE'></center></pre></bdo></b><th id='1IkFE'></th></span></q></dt></tr></i><div id='1IkFE'><tfoot id='1IkFE'></tfoot><dl id='1IkFE'><fieldset id='1IkFE'></fieldset></dl></div>
              • <bdo id='1IkFE'></bdo><ul id='1IkFE'></ul>
                  <tfoot id='1IkFE'></tfoot>
                1. 本文介绍了如何将 CSV 文件导入 MySQL 表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个来自客户端的非规范化事件日志 CSV,我试图将其加载到 MySQL 表中,以便我可以重构为合理的格式.我创建了一个名为CSVImport"的表,该表为 CSV 文件的每一列都有一个字段.CSV 包含 99 列,因此这本身就是一项艰巨的任务:

                  CREATE TABLE 'CSVImport' (id INT);ALTER TABLE CSVImport ADD COLUMN Title VARCHAR(256);ALTER TABLE CSVImport ADD COLUMN Company VARCHAR(256);ALTER TABLE CSVImport ADD COLUMN NumTickets VARCHAR(256);...ALTER TABLE CSVImport Date49 ADD COLUMN Date49 VARCHAR(256);ALTER TABLE CSVImport Date50 ADD COLUMN Date50 VARCHAR(256);

                  表上没有约束,所有字段都包含 VARCHAR(256) 值,除了包含计数(用 INT 表示)、是/否(用 BIT 表示)、价格(用 DECIMAL 表示)和文字简介(由 TEXT 表示).

                  我尝试将数据加载到文件中:

                  LOAD DATA INFILE '/home/paul/clientdata.csv' INTO TABLE CSVImport;查询 OK,2023 行受影响,65535 条警告(0.08 秒)记录:2023 删除:0 跳过:0 警告:198256选择 * 从 CSVImport;|空 |空 |空 |空 |空 |...

                  整个表被NULL填满.

                  我认为问题在于文本简介包含不止一行,并且 MySQL 正在解析文件,好像每个新行都对应于一个数据库行.我可以毫无问题地将文件加载到 OpenOffice 中.

                  clientdata.csv 文件包含 2593 行和 570 条记录.第一行包含列名.我认为它是逗号分隔的,文本显然是用双引号分隔的.

                  更新:

                  如有疑问,请阅读手册:

                  I have an unnormalized events-diary CSV from a client that I'm trying to load into a MySQL table so that I can refactor into a sane format. I created a table called 'CSVImport' that has one field for every column of the CSV file. The CSV contains 99 columns , so this was a hard enough task in itself:

                  CREATE TABLE 'CSVImport' (id INT);
                  ALTER TABLE CSVImport ADD COLUMN Title VARCHAR(256);
                  ALTER TABLE CSVImport ADD COLUMN Company VARCHAR(256);
                  ALTER TABLE CSVImport ADD COLUMN NumTickets VARCHAR(256);
                  ...
                  ALTER TABLE CSVImport Date49 ADD COLUMN Date49 VARCHAR(256);
                  ALTER TABLE CSVImport Date50 ADD COLUMN Date50 VARCHAR(256);
                  

                  No constraints are on the table, and all the fields hold VARCHAR(256) values, except the columns which contain counts (represented by INT), yes/no (represented by BIT), prices (represented by DECIMAL), and text blurbs (represented by TEXT).

                  I tried to load data into the file:

                  LOAD DATA INFILE '/home/paul/clientdata.csv' INTO TABLE CSVImport;
                  Query OK, 2023 rows affected, 65535 warnings (0.08 sec)
                  Records: 2023  Deleted: 0  Skipped: 0  Warnings: 198256
                  SELECT * FROM CSVImport;
                  | NULL             | NULL        | NULL           | NULL | NULL               | 
                  ...
                  

                  The whole table is filled with NULL.

                  I think the problem is that the text blurbs contain more than one line, and MySQL is parsing the file as if each new line would correspond to one databazse row. I can load the file into OpenOffice without a problem.

                  The clientdata.csv file contains 2593 lines, and 570 records. The first line contains column names. I think it is comma delimited, and text is apparently delimited with doublequote.

                  UPDATE:

                  When in doubt, read the manual: http://dev.mysql.com/doc/refman/5.0/en/load-data.html

                  I added some information to the LOAD DATA statement that OpenOffice was smart enough to infer, and now it loads the correct number of records:

                  LOAD DATA INFILE "/home/paul/clientdata.csv"
                  INTO TABLE CSVImport
                  COLUMNS TERMINATED BY ','
                  OPTIONALLY ENCLOSED BY '"'
                  ESCAPED BY '"'
                  LINES TERMINATED BY '\n'
                  IGNORE 1 LINES;
                  

                  But still there are lots of completely NULL records, and none of the data that got loaded seems to be in the right place.

                  解决方案

                  The core of your problem seems to be matching the columns in the CSV file to those in the table.

                  Many graphical mySQL clients have very nice import dialogs for this kind of thing.

                  My favourite for the job is Windows based HeidiSQL. It gives you a graphical interface to build the LOAD DATA command; you can re-use it programmatically later.

                  Screenshot: "Import textfile" dialog

                  To open the Import textfile" dialog, go to Tools > Import CSV file:

                  这篇关于如何将 CSV 文件导入 MySQL 表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在mysql中插入多行 下一篇:MySQL 查询在逗号分隔的字符串中查找值

                  相关文章

                  最新文章

                  <small id='B4MbG'></small><noframes id='B4MbG'>

                    1. <i id='B4MbG'><tr id='B4MbG'><dt id='B4MbG'><q id='B4MbG'><span id='B4MbG'><b id='B4MbG'><form id='B4MbG'><ins id='B4MbG'></ins><ul id='B4MbG'></ul><sub id='B4MbG'></sub></form><legend id='B4MbG'></legend><bdo id='B4MbG'><pre id='B4MbG'><center id='B4MbG'></center></pre></bdo></b><th id='B4MbG'></th></span></q></dt></tr></i><div id='B4MbG'><tfoot id='B4MbG'></tfoot><dl id='B4MbG'><fieldset id='B4MbG'></fieldset></dl></div>
                      <legend id='B4MbG'><style id='B4MbG'><dir id='B4MbG'><q id='B4MbG'></q></dir></style></legend>
                      <tfoot id='B4MbG'></tfoot>

                        <bdo id='B4MbG'></bdo><ul id='B4MbG'></ul>