求助,代码如下:
@echo off
setlocal enabledelayedexpansion
set input_file=".\userlist.csv"
if not exist "%input_file%" (
echo "Error: Input file not found."
exit /b 1
)
set output_file="userlist.sql"
(for /F "usebackq delims=" %%a in (".\userlist.csv") do (
set "line=%%a"
set "line=!line:,=','!"
echo "INSERT userlist VALUES ('!line:~1,-1!'');"
)) > "%output_file%"
echo "CONVERSION COMPLETE."
endlocal
pause
有两个问题没解决:1、sql的空值的表达格式为NULL,但是批处理生成的是'' 。2、 echo "INSERT userlist VALUES ('!line:~1,-1!'');"这一行,生成的sql文件每一行都前后都会有双引号,实际是不需要的,但批处理语句中去除双引号后,就不工作了。
请大佬不吝指教。
ps 因为传不了附件,我贴出userlist.csv内容,保存记事本后改名即可。
UserId,UserNo,UserPass,UserTName,UserDetail,UserTel,UserMobile,UserEmail,UserIcon,UserSex,UserZW,UserAge,UserRemark,UserAddress,UserWebSite,UserRole,UserLevel,UserState,UserLastLoginDateTime,UserCreateTime,UserRestrict
6,6,d41d8cd98f00b204e9800998ecf8427e,6,,,,,,0,,0,,,,Users,1,1,,,
7,7,d41d8cd98f00b204e9800998ecf8427e,7,,,,,,0,,0,,,,Users,5,1,,,
8,8,d41d8cd98f00b204e9800998ecf8427e,8,,,,,,0,,0,,,,Users,5,1,,,
9,9,d41d8cd98f00b204e9800998ecf8427e,9,,,,,,0,,0,,,,Users,5,1,,,
10,10,d41d8cd98f00b204e9800998ecf8427e,10,,,,,,0,,0,,,,Users,5,1,,,
|