因为我正在使用工作台并且想要加载文本文件数据。 我想在列中添加文件名。
让我举个例子。
我的文字档案:20170205077.txt
" 20170205077.txt"中的数据:
1|Something|Something_2, 1|Something|Something_3
查询也是
LOAD DATA LOCAL INFILE "20170205077.txt" INTO "DB.TABLE"
FIELDS TERMINATED BY '|' Lines terminated by ', '
执行时我希望这样的表格如下:
| FileName |userId| data | data |
|20170205077| 1 |Something|Something_2|
|20170205077| 1 |Something|Something_3|
如何增强查询?
答案 0 :(得分:0)
创建一个bat文件:(例如upload.bat) 将它和所有文本文件放在C:\ import中(文件夹和文件的名称不能包含SPACE)。然后运行bat文件导入
@echo off
setlocal enabledelayedexpansion
FOR %%f IN ("*.txt") DO (
set old=%%~dpnxf
set new=!old:\=\\!
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql" -e "load data local infile '"!new!"' IGNORE into table DB.TABLE (userID, data1, data2) FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"""' LINES TERMINATED BY ',' SET Filename = %%nf" -h yourhost -u youruser -pYourpassword YourDB
echo UPLOAD %%~nxf DONE
)
pause