如何在 Bash 脚本中自动引用 SQL INSERT STATEMENT 的“字符串值”
Posted
技术标签:
【中文标题】如何在 Bash 脚本中自动引用 SQL INSERT STATEMENT 的“字符串值”【英文标题】:How to auto-quote "string values" of a SQL INSERT STATEMENT in Bash script 【发布时间】:2021-06-17 08:27:56 【问题描述】:我编写了一个简单的 Bash 脚本,它从完全由整数组成的 CSV 数据中为多行生成 SQL INSERT STATEMENT,并将其保存到文本文件中(这样我就可以将其复制并粘贴到任何我想要的地方)。
现在我想知道如何自动引用字符串值;即自动包装数据类型为字符串且需要 "" 才能插入 SQL 表的 CSV 列。正如您在下面看到的,脚本读取整个 CSV 行并且不会区分列。我是否必须将每一列分配给一个变量,然后分别引用字符串?或者有没有更有效的方法?无论哪种方式,我都会很高兴看到您对上述问题的修复!
#!/bin/bash
echo Path to to-be-imported CSV:
read csv_file
echo Table name to import into:
read table
echo "INSERT INTO $table VALUES" > SQL_INSERT_$table.txt
while read line
do
echo "($line),"
done < <(tail -n +2 $csv_file) >> SQL_INSERT_$table.txt && sed -i '' '$ s/.$/;/' SQL_INSERT_$table.txt
【问题讨论】:
您使用的是哪个数据库?也许这些答案会有所帮助 - ***.com/questions/13405572/… - 即数据库知道它期望什么类型 - 这样你就可以知道要引用什么了.. 重复问题***.com/questions/66738498/… @MrR:我使用 mysql 5.7。我仍然需要在我的 INSERT STATEMENT 中引用字符串值,它可能有数百行长。这就是为什么我要尝试对它们进行脚本引用,然后将结果复制并粘贴到 MySQL 查询框中。 您需要解决 2 个部分 - (a) 要引用的列,以及 (b) 然后引用每一列.. 尽管 mysql 没有导入器(请参阅 ***.com/questions/6605765/…对于其他一些替代方案)。 【参考方案1】:这是上面脚本的修改版本,它成功地自动引用了字符串列:
#!/bin/bash
# Prompt for input and: 1. enter CSV path to be imported into DB; 2. its equivalent table name in the DB
echo Path to CSV:
read CSV_file
echo DB table name to import into:
read DB_table
# Create .txt file that will contain SQL INSERT STATEMENT and enter DB table name
echo "INSERT INTO $DB_table VALUES" > SQL_INSERT_$DB_table.txt
# Auto-quote string columns in the .txt file, leaving other columns intact
awk -F, 'OFS=FS for (i=1;i<=NF;i++) if (match($i, /^[0-9.-]+$/)==0) printf "\"" $i "\"" else printf $i; if (i<NF) printf OFS; printf "\n"' $CSV_file > temp.txt
# read-while loop to populate INSERT STATEMENT row values from CSV (2nd row to the end) and replace final comma with semicolon for those RDBMS's that require a concluding semicolon at the end of SQL STATEMENT
while read line
do
echo "($line),"
done < <(tail -n +2 temp.txt) >> SQL_INSERT_$DB_table.txt && sed -i '' '$ s/.$/;/' SQL_INSERT_$DB_table.txt
# Delete temporary .txt file that contained auto-quoted string values
rm temp.txt
这是脚本的更完整版本,它不仅自动引用字符串列,而且在 SQL INSERT STATEMENT 中列出列名:
#!/bin/bash
# Prompt for input and: 1. enter CSV path to be imported into DB; 2. its equivalent table name in the DB
echo Path to CSV:
read CSV_file
echo DB table name to import into:
read DB_table
# Create .txt file that will contain SQL INSERT STATEMENT and enter DB table name
echo "INSERT INTO $DB_table (" > SQL_INSERT_$DB_table.txt
# List out CSV header as INSERT STATEMENT column names and append to .txt file
echo "`head -n 1 $CSV_file`)" >> SQL_INSERT_$DB_table.txt
# Auto-quote string columns in the .txt file, leaving other columns intact
awk -F, 'OFS=FS for (i=1;i<=NF;i++) if (match($i, /^[0-9.-]+$/)==0) printf "\"" $i "\"" else printf $i; if (i<NF) printf OFS; printf "\n"' $CSV_file > temp.txt
echo "VALUES" >> SQL_INSERT_$DB_table.txt
# read-while loop to populate INSERT STATEMENT row values from CSV (2nd row to the end) and replace final comma with semicolon for those RDBMS's that require a concluding semicolon at the end of SQL STATEMENT
while read line
do
echo "($line),"
done < <(tail -n +2 temp.txt) >> SQL_INSERT_$DB_table.txt && sed -i '' '$ s/.$/;/' SQL_INSERT_$DB_table.txt
# Delete temporary .txt file that contained auto-quoted string values
rm temp.txt
【讨论】:
以上是关于如何在 Bash 脚本中自动引用 SQL INSERT STATEMENT 的“字符串值”的主要内容,如果未能解决你的问题,请参考以下文章
在 bash 脚本中,如何引用单独的文件以在 for 循环中使用它? [复制]