postgresql shell脚本传递参数并执行sql脚本并

参考:

https://stackoverflow.com/questions/7389416/postgresql-how-to-pass-parameters-from-command-line

https://blog.csdn.net/dazuiba008/article/details/79268537  (推荐)

######################################数据库表结构######################################
# name character

#######################################命令行执行#######################################
# shell命令连接postgres数据库
psql 数据库名 用户名 -v name="Hello World"
# 接收变量 注意: 如果参数是字符串类型那么接收的时候必须是 :'变量名' 这种形式;如果是数字类则为 :变量名
select * from tableName where name = :'name';

#################################命令行执行sql脚本(查询操作)###########################
# shell命令连接postgres数据库
psql 数据库名 用户名 -v name="Hello World" -f test.sql
# test.sql 注意: 如果参数是字符串类型那么接收的时候必须是 :'变量名' 这种形式;如果是数字类则为 :变量名
select * from tableName where name = :'name';

#################################命令行执行sql脚本(插入操作)###########################
# shell命令连接postgres数据库
psql 数据库名 用户名 -v name="Hello World" -f test.sql
# test.sql 注意:1.tableName后面必须跟上全列名 2.如果参数是字符串类型那么接收的时候必须是 :'变量名' 这种形式;如果是数字类则为 :变量名
insert into tableName(name) where name = :'name';

 

字符串操作

      参考:https://www.postgresql.org/docs/9.1/static/functions-string.html  

 

posted @ 2018-07-08 16:18  lvlin241  阅读(4813)  评论(0编辑  收藏  举报