Home > Backend Development > PHP Tutorial > 如何将csv文件导入到MySQL

如何将csv文件导入到MySQL

PHPz
Release: 2020-06-23 11:44:01
Original
2218 people have browsed it

如何将csv文件导入到MySQL

如何将csv文件导入到MySQL?

将csv文件导入到MySQL

首先登录MySQL:

mysql -u root -p123456
Copy after login

创建数据表,保证字段和csv文件中的列一致

mysql> show DATABASES;
mysql> use TEST;
mysql> show TABLES;
mysql> CREATE TABLE mytable(
    -> id INT NOT NULL AUTO_INCREMENT,
    -> Qtime DATETIME NOT NULL,
    -> Tprice FLOAT NOT NULL,
    -> PRIMARY KEY ( id )
    -> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

查看安全路径,也就是要将上传文件放到安全路径下,才能成功上传

mysql> show variables like '%secure%';
+--------------------------+-----------------------+
| Variable_name            | Value                 |
+--------------------------+-----------------------+
| require_secure_transport | OFF                   |
| secure_auth              | ON                    |
| secure_file_priv         | /var/lib/mysql-files/ |
+--------------------------+-----------------------+
Copy after login

在终端中复制文件到安全路径

sudo cp /home/hadoop/文档/文件.csv /var/lib/mysql-files
Copy after login

然后在到mysql下执行

mysql> load data infile '/var/lib/mysql-files/文件.csv'  into table mytable fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\n';
Copy after login

导出文件类似

mysql> select * from `table` load data infile '/文档/test.csv' fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\n';
Copy after login

更多相关技术文章,请访问PHP中文网

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template