我有一个 Windows批处理文件,可以读取其目录中的所有CSV文件。我遇到的问题是CSV的行在时间戳中有双引号,还包括一个微量。我很确定下面的代码必须关闭,但它要么导入一行并在timestamp列中转储NULL,要么表示“SET此时是意外的”,我认为是由于未转义的字符因为它是作为 Windows批处理文件运行的,我的代码低于和低于这是CSV文件数据的样本。
echo on
setlocal enabledelayedexpansion
FOR %%f IN ("*.csv") DO (
set old=%%~dpnxf
set new=!old:\=\\!
mysql -e "LOAD DATA local INFILE '"!new!"' IGNORE into table db.table COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' (prodID,@timeStamp,Ch1,Ch2,Ch3,Ch4,Ch5,Ch6,Ch7,Ch8) SET timeStamp=STR_TO_DATE(@timeStamp,'%%Y-%%m-%%d %%H:%%i:%%s.%%f')" -u user -ppass
echo %%~nxf DONE
)
12499,"2014-01-28 11:00:00.0",0,0,0,0,0,5,0,0
12499,"2014-01-28 12:00:00.0",0,4,0,0,1,0,0,2
12499,"2014-01-28 13:00:00.0",0,1,0,0,4,0,0,4
12499,"2014-01-28 14:00:00.0",0,0,0,0,0,0,0,7
12499,"2014-01-28 15:00:00.0",0,0,0,0,3,0,0,2
答案 0 :(得分:1)
The problem here is that the MySQL import process requires the "%" character be used in date/time formatting. Unfortunately, that character is also claimed by the Windows batch process. The real question is "how to escape the percent characters in the SQL statement such that they are ignored by the for loop in the batch file, i.e. get sent through to MySQL unchanged?" The answer that seemed to work for me was to use %%^ in the SQL statement, thus:
echo on
setlocal enabledelayedexpansion
FOR %%f IN ("*.csv") DO (
set old=%%~dpnxf
set new=!old:\=\\!
mysql -e "LOAD DATA local INFILE '"!new!"' IGNORE into table db.table COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' (prodID,@timeStamp,Ch1,Ch2,Ch3,Ch4,Ch5,Ch6,Ch7,Ch8) SET timeStamp=STR_TO_DATE(@timeStamp,'%%^Y-%%^m-%%^d %%^H:%%^i:%%^s.%%^f')" -u user -ppass
echo %%~nxf DONE
)
答案 1 :(得分:0)
@ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
pushd "D:\bat\SO\files"
FOR %%f IN ("*.csv") DO (
if /I "%%~nxf"=="31212125.csv" (
set "old=%%~dpnxf"
set "new=!old:\=\\!"
echo old="!old!" new="!new!"
echo(
FOR /F "usebackq tokens=1,2* delims=," %%G IN ("!old!") DO (
rem mysql -e "LOAD DATA local INFILE '"!new!"' IGNORE into table db.table COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' (prodID,@timeStamp,Ch1,Ch2,Ch3,Ch4,Ch5,Ch6,Ch7,Ch8) SET timeStamp=STR_TO_DATE(@timeStamp,'%%Y-%%m-%%d %%H:%%i:%%s.%%f')" -u user -ppass
echo G=%%~G [H]=[%%~H] I=%%~I
)
) else rem echo skipped %%~nxf
)
popd
上面的脚本显示了如何解析输入文件行的方式。操作mysql
命令被注释为未更改,因为问题是关于.bat
脚本而不是mysql
。请注意,if /I "%%~nxf"=="31212125.csv"
仅用于跳过我的其他测试CSV 。
<强>输出强>:
==>type D:\bat\SO\files\31212125.csv
12499,"2014-01-28 11:00:00.0",0,0,0,0,0,5,0,0
12500,"2014-01-28 12:00:00.0",0,4,0,0,1,0,0,2
12501,"2014-01-28 13:00:00.0",0,1,0,0,4,0,0,4
12502,"2014-01-28 14:00:00.0",0,0,0,0,0,0,0,7
12503,"2014-01-28 15:00:00.0",0,0,0,0,3,0,0,2
==>D:\bat\SO\31212125.bat
old="D:\bat\SO\files\31212125.csv" new="D:\\bat\\SO\\files\\31212125.csv"
G=12499 [H]=[2014-01-28 11:00:00.0] I=0,0,0,0,0,5,0,0
G=12500 [H]=[2014-01-28 12:00:00.0] I=0,4,0,0,1,0,0,2
G=12501 [H]=[2014-01-28 13:00:00.0] I=0,1,0,0,4,0,0,4
G=12502 [H]=[2014-01-28 14:00:00.0] I=0,0,0,0,0,0,0,7
G=12503 [H]=[2014-01-28 15:00:00.0] I=0,0,0,0,3,0,0,2
==>