How can I load many files into mysql automatically add date information?

偶尔善良 提交于 2019-12-12 03:39:12

问题


I have many files, for example: 20170319, 20170320 ... For each file I have 2 columns, one for username and the other was data. I've created a table

create table A(user varchar(35), date date, data varchar(35), primary key(user, date));

Then, I want to load those files into database, and use filename as specific date in date field.

Can I still use sth like:

Load data infile '20170320' into table A

回答1:


The answer is that you cannot do this in MySQL alone, you need to use an external program or script that builds the load data infile statements with the appropriate SET clause derived from the name of the file:

The SET clause can be used to supply values not derived from the input file. The following statement sets column3 to the current date and time:

LOAD DATA INFILE 'file.txt'
  INTO TABLE t1
  (column1, column2)
  SET column3 = CURRENT_TIMESTAMP;

The reason for this is that:

  1. load data infile cannot use the file name as an input variable
  2. neither MySQL prepared statements using the prepare statement, nor stored procedures are allowed to use the load data infile statement.


来源:https://stackoverflow.com/questions/42923999/how-can-i-load-many-files-into-mysql-automatically-add-date-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!