csv 导入数据库 转表

1、准备工作:建表、准备csv表数据

(1)建表:

CREATE TABLE `task_import`(
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `time` DATE DEFAULT NULL,
    `code` VARCHAR(60) DEFAULT NULL,
    `name` VARCHAR(80) DEFAULT NULL,
    PRIMARY KEY (`id`)
);

(2)准备csv表数据

aa.jpg

2、然后就是导入了

在这里,遇到了一个bug,用sqlyog直接导入会报错,然后把它的代码复制下来,

修改前:
load data local infile '路径\\task_import.csv' 
into table `tasks`.`task_import` 
fields escaped by '\\' terminated by ',' lines terminated by '\r\n' (`id`, `time`, `code`, `name`)
修改后:
LOAD DATA LOCAL INFILE '路径\\task_import.csv' 
INTO TABLE `task_import` CHARACTER SET gb2312
FIELDS ESCAPED BY '\\' TERMINATED BY ',' LINES TERMINATED BY '\r\n' (`id`, `time`, `code`, `name`)

找到的原因:因为字符集的问题,我的数据库默认了utf-8,所以导不进去,最后设置了字符集编码格式就好了

3、结果

bb.jpg

4、字符解释

摘取一下别人的:https://blog.csdn.net/lee18254290736/article/details/81011930

escaped by 分为两种情况,

(1)escaped by ','
(2)escaped by ' " '
一般 escaped by ',' 与 lines terminated by '\r\n' 有对应关系,如果用escaped by ' " ',即引号分隔,则只有将lines terminated by改成'\n'才能导入,并且此时不能ignore 1 lines.

terminated by 分隔符:意思是以什么字符作为分隔符
enclosed by 字段括起字符
escaped by 转义字符
terminated by 描述字段的分隔符,默认情况下是tab字符(\t)
enclosed by 描述的是字段的括起字符。
escaped by 描述的转义字符。默认的是反斜杠(backslash:\ )

你可能感兴趣的:(csv 导入数据库 转表)