我编写了一个 Bash 脚本来将值插入到SQLite https://en.wikipedia.org/wiki/SQLite数据库。命令如下:
sqlite3 ${db_name} "insert into ${table_name} (${column1},${column2}) values ('$f1','$f2');"
该命令可以正常工作,直到f1
变量包含单引号:
# E.g., f1="I'm just kidding"
# The command reported an error:
Error: near "m": syntax error
我们如何转义变量内的单引号?
要转义 SQL 的单引号,请将其加倍 (https://www.sqlite.org/faq.html#q14 https://www.sqlite.org/faq.html#q14):
$ f1="I'm just kidding"
$ echo "${f1//\'/''}"
I''m just kidding
$ f2="no single quotes"
$ echo "${f2//\'/''}"
no single quotes
So
sqlite3 ${db_name} "insert into ${table_name} (${column1},${column2}) values ('${f1//\'/''}','${f2//\'/''}');"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)