使用shell脚本将数据插入postgres db

5n0oy7gb  于 11个月前  发布在  Shell
关注(0)|答案(1)|浏览(269)

无法使用shell脚本插入包含字符的数据。下面是我的shell脚本的内容

export PGPASSWORD='postgres'; psql -h 'x.x.x.x' -U 'postgres' -d 'postgres' -c 'create table luck3 (empId int,name char(50));'
export PGPASSWORD='postgres'; psql -h 'x.x.x.x' -U 'postgres' -d 'postgres' -c 'insert into luck3 values(9,'sam');'
export PGPASSWORD='postgres'; psql -h 'x.x.x.x' -U 'postgres' -d 'postgres' -c 'insert into luck3 values(6,'ram');'

字符串
当数据类型为整数时,该命令工作正常,但是当我插入像'sam'这样的数据时,它会抛出一个错误:column 'sam' does not exist

ycl3bljg

ycl3bljg1#

export PGPASSWORD ='postgres'; psql -h 'x.x.x.x' -U 'postgres' -d 'postgres' -c 'insert into luck 3 values(6,' sam ');'您在脚本中到处使用半引号。因此,需要在查询中对其进行转义:

-c 'insert into luck3 values(6,\'sam\');'

字符串
或者为了更好的阅读(理解),使用双引号来“转义"查询:

-c "insert into luck3 values(6,'sam');"

相关问题