如何使用特定的txt文件创建表格?

时间:2019-06-03 08:56:22

标签: mysql sql

我要加载的文件如下:/04:2016:27/$/06,54,05/$/Page/$/Bob/$/24/$/Bax/$/Reper 10/

我收到错误

  

错误代码:1292。日期时间值不正确:”第1行的“ data_achizitie”列

create table Tabela_veche(
    data_achizitie date,
    numere varchar(255),
    nume varchar (255),
    prenume varchar(255),
    bucati int,
    unit_mas varchar(255),
    reper varchar(255),
    fulldate datetime);

AND

LOAD DATA INFILE 
    'C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\fisier.txt'
    INTO TABLE Tabela_veche
    CHARACTER SET latin1
    FIELDS TERMINATED BY '/'
    OPTIONALLY ENCLOSED BY '$'
    LINES TERMINATED BY '\r\n'; 

请帮助! 谢谢!

1 个答案:

答案 0 :(得分:0)

在dev.mysql.com/doc/refman/8.0/en/load-data.html中查看“输入预处理”部分-在您的情况下,将data列分配给变量并在写入表之前对其进行格式化。 而且您终止并可能用括起来的行是错误的处理方法。注意str_to_date函数。 (基本上所有sql函数都可以在set语句中使用)。

LOAD DATA INFILE 
    'C:\\Program Files\\MariaDB 10.1\\data\\sandbox\\data.txt'
    INTO TABLE Tabela_veche
    CHARACTER SET latin1
    FIELDS TERMINATED BY '$'
    OPTIONALLY ENCLOSED BY '/'
    LINES TERMINATED BY '\r\n'
(@dt,   
    numere,   nume,  prenume ,    bucati ,  unit_mas ,    reper  ) 
     set data_achizitie = str_to_date(@dt,'%m:%Y:%d')
;

顺便说一句,您没有在数据中提供完整日期,因此您需要指定要加载的列。

+----------------+----------+------+---------+--------+----------+----------+----------+
| data_achizitie | numere   | nume | prenume | bucati | unit_mas | reper    | fulldate |
+----------------+----------+------+---------+--------+----------+----------+----------+
| 2016-04-27     | 06,54,05 | Page | Bob     |     24 | Bax      | Reper 10 | NULL     |
+----------------+----------+------+---------+--------+----------+----------+----------+
1 row in set (0.00 sec)