我有一些csv文件,我需要打开csv文件,读取csv的第一行并将其转换为临时sql表,然后将数据加载到sql表中,如下所示:
阅读CSV行和每行:
将其分解为字段创建一个临时sql表
将这些字段插入数据库表的一行
我试过这样的事情
这个脚本现在分为4个部分,文件初始化;文件创建,处理和复制数据, 一切都工作正常,除了在fil.sql上我输出为
CREATE TEMP TABLE temtab(
firstcolumn character varying (255),
secondcolumn character varying (255),
lastcolumn character varying (255),
);
\COPY temtab from bio.csv WITH DELIMITER ; csv HEADER
虽然我想要最后一个字母没有逗号
CREATE TEMP TABLE temtab (
firstcolumn character varying (255),
secondcolumn character varying (255),
lastcolumn character varying (255)
);
\COPY temtab from bio.csv WITH DELIMITER ; csv HEADER
@echo off
::setlocal enabledelayedexpansion
REM Assiging dir to current directory
SET dir=%CD%
REM Defining database name
SET dbname=****
REM Defining Host name
SET host=****
REM Defining user
SET user=****
REM Defining Port
SET port=****
REM SQL file where query is to be executed
SET sqfile=fil.sql
SET fi=bio.csv
call:fileinitialization
call:filecreation
call:proces
call:copydata
goto:eof
:fileinitialization
REM Assigning name of temporary table
SET tabnam=temtab
REM Setting delimiter to variable delim
SET delim=;
REM Declaring variable numfields to store index of variable names array
set numFields=0
echo para setted
set fi=bio.csv
SET tex=text
SET com=,
GOTO:EOF
:filecreation
REM Setting create temporary table command with table name tabnam
SET creat=CREATE TEMP TABLE %tabnam%
echo %creat%
GOTO:EOF
:proces
REM Executing loop for each file in current directory
echo %creat%>fil.sql
REM Read the lines of the CSV file
For /F "eol==" %%A in (bio.csv) Do ( set "line=%%A"
REM check if index of array is 0
if !numFields! equ 0 (
REM Fisrt line, Store in array name
for %%B in (!line: ^=!) do (
echo %%B character varying (255^),>>fil.sql
set /A numFields+=1
set name[!numFields!]=%%B
) ) )
GOTO:EOF
:copydata
echo \COPY %tabnam% from %fi% WITH DELIMITER %delim% csv HEADER
echo \COPY %tabnam% from %fi% WITH DELIMITER %delim% csv HEADER;>>fil.sql
GOTO:EOF
::endlocal
Pause
答案 0 :(得分:1)
虽然我不知道SQL表的格式,但我可以向您展示如何读取CSV文件。下面的批处理文件读取文件中的所有行;它首先从第一行(CSV标题)获取字段名称并创建变量名称的数组(消除字段名称中的可能空格);然后它读取剩余的行并将每个字段值分配给相应的批处理变量。
ProcessCSV.BAT:
@echo off
rem General-purpose CSV file reader program
rem Antonio Perez Ayala
setlocal EnableDelayedExpansion
set numFields=0
rem Read the lines of the CSV file
for /F "delims=" %%a in (CSVfile.csv) do (
set "line=%%a"
if !numFields! equ 0 (
rem It is the first line: break it into an array of field names (removing spaces)
for %%b in (!line: ^=!) do (
set /A numFields+=1
set name[!numFields!]=%%b
)
) else (
rem Replace spaces by Ascii-128 (to avoid split values that may have spaces)
set "line=!line: =Ç!"
rem Insert any char. at beginning of each field, and separate fields with spaces
set i=0
for %%b in (X!line:^,^= X!) do (
set "field=%%b"
rem Recover spaces in this field, if any
set "field=!field:Ç= !"
rem And assign it to corresponding variable (removing first character)
set /A i+=1
for %%i in (!i!) do set "!name[%%i]!=!field:~1!"
)
rem At this point all variables have the values of current record.
rem They may be accessed explicitly (ie, from example CSVfile.csv):
echo/
echo Record of !FirstName! !LastName!
rem ... or implicilty via the NAME array:
for /L %%i in (3,1,!numFields!) do (
for %%b in (!name[%%i]!) do echo %%b: !%%b!
)
)
)
CSVfile.csv:
First Name,Last Name,Address,Postal Code,Company,Departament,Floor,Phone,Mobile
John,Smith,123 Fake Street,45612,SomeCo,Accounting,4,123-555-5555,123-555-5556
Jane,Doe,123 Fake Street,,SomeCo,,4,123-555-5555,123-555-5556
输出:
Record of John Smith
Address: 123 Fake Street
PostalCode: 45612
Company: SomeCo
Departament: Accounting
Floor: 4
Phone: 123-555-5555
Mobile: 123-555-5556
Record of Jane Doe
Address: 123 Fake Street
PostalCode:
Company: SomeCo
Departament:
Floor: 4
Phone: 123-555-5555
Mobile: 123-555-5556
请注意,此程序使用多种高级批处理技术。我建议你在你不完全理解的每个命令上得到帮助(例如:SET /?)并仔细阅读。如果在此过程之后您对此计划有其他疑问,请将其作为原始问题的修改发布。
该程序最复杂的部分负责在相应字段为空时将空字符串分配给变量(两个逗号并排);如果文件没有空字段,程序可能会稍微简单一些。此外,如果文件中出现某些特殊批处理字符,此程序(如大多数批处理解决方案)可能会产生错误结果,例如!如果需要,可以通过程序中的某些修改来管理大多数这些字符。
编辑: 当没有空字段时修改后的版本
@echo off
rem CSV file reader program when no empty fields exist
rem Antonio Perez Ayala
setlocal EnableDelayedExpansion
set numFields=0
rem Read the lines of the CSV file
for /F "delims=" %%a in (CSVfile.csv) do (
set "line=%%a"
if !numFields! equ 0 (
rem It is the first line: break it into an array of field names (removing spaces)
for %%b in (!line: ^=!) do (
set /A numFields+=1
set name[!numFields!]=%%b
)
) else (
rem Replace spaces by Ascii-128 (to avoid split values that may have spaces)
set "line=!line: =Ç!"
rem Separate fields (using comma as standard Batch separator)
set i=0
for %%b in (!line!) do (
set "field=%%b"
rem Assign this field to corresponding variable, recovering spaces
set /A i+=1
for %%i in (!i!) do set "!name[%%i]!=!field:Ç= !"
)
rem At this point all variables have the values of current record.
rem They may be accessed explicitly (ie, from example CSVfile.csv):
echo/
echo Record of !FirstName! !LastName!
rem ... or implicilty via the NAME array:
for /L %%i in (3,1,!numFields!) do (
for %%b in (!name[%%i]!) do echo %%b: !%%b!
)
)
)
请注意,除了空格之外,FOR集中的标准分隔符是逗号,分号和等号:
for %a in (one two,three;four=five) do echo %a
上一个程序用另一个字符替换空格,并使用逗号分隔字段。但是,如果该行可能包含分号或等号,则该字段将在该点被分割,因此在这种情况下,必须在FOR之前为其他字符更改这些字符,然后以与空间相同的方式恢复。
编辑: 新请求的修改(删除最后一个逗号)
消除最后一个逗号并非微不足道,尽管也不是太复杂。我希望我的方法易于理解;它基于显示文本的SET / P命令行为(输入提示),结尾没有新行;请注意格式为SET /P =text>>out<NUL
。需要<NUL
部分,因此SET / P不会等待输入;不要在<
之前留空格(与>>
相同)。但是,我认为这种行为在Windows Vista和更高版本中不起作用。如果该方法不适合您,则必须再次修改...
我也向前迈进了一步,包括一些关于你的代码中仍然缺少的部分的评论(我认为),即处理几个文件。
:proces
REM Executing loop for each file in current directory
REM *This may be done with a FOR loop:*
::*for %%F in (*.csv) do (*
REM *The file name is given by %%F. In this case, the fileinitialization part*
REM *must be done here, for example:*
set numFields=0
echo %creat%>fil.sql
REM Read the lines of the CSV file
For /F "eol==" %%A in (bio.csv) Do (
set "line=%%A"
REM check if index of array is 0
if !numFields! equ 0 (
REM First line, Store in array name
for %%B in (!line: ^=!) do (
REM Note that I changed the place of the ECHO command
set /A numFields+=1
set name[!numFields!]=%%B
if !numFields! equ 1 (
REM First field: show it with NO comma and NO NEW LINE
set /P =%%B (text^)>>%sqfile%<NUL
) else (
REM Next fields: complete the comma of previous field, WITH NEW LINE
echo ,>>%sqfile%
REM ... and show this field with NO comma and NO NEW LINE (again)
set /P =%%B (text^)>>%sqfile%<NUL
)
)
REM Insert the new line of last field (that have NOT comma :-)
echo/>>%sqfile%
)
)
::*)*
GOTO:EOF
:copydata
我强烈建议您保留以前的格式:括在括号中的每个代码块中的4个对齐列,并将右括号放在打开命令FOR或IF的同一列中。此格式将帮助您轻松找到大型程序中括号不匹配导致的错误。
安东尼奥