Postgres 命令行语句:INSERT 抱怨不存在的列
我有以下脚本,它通过 ssh 连接到远程服务器,并在其 Postgresql 数据库上发出 SQL 语句:
#!/bin/bash
db_query() {
app_server="$1"
sql_stmt="$2"
psql_cmd="psql -d vdc --pset tuples_only -c '$sql_stmt'"
rows_count=`ssh $app_server "sudo su - postgres -c \"$psql_cmd\""`
echo "DB_QUERY: rows_count = $rows_count"
}
现在我尝试向下表发出 SELECT 和 INSERT 语句:
CREATE TABLE pb_properties
(
c_name character varying(255) NOT NULL,
c_value character varying(255),
CONSTRAINT pb_properties_pkey PRIMARY KEY (c_name)
)
如果我执行 SELECT 语句,此函数可以正常工作:
#!/bin/bash
source db_query.sh
db_query staging "SELECT * FROM pb_properties;"
>> Output: DB_QUERY: rows_count = support-email | [email protected]
但是如果我执行 INSERT 语句,它不起作用:
#!/bin/bash
source db_query.sh
db_query prestaging "INSERT INTO pb_properties (c_name, c_value) VALUES ('support-email', '[email protected]');"
>> Output:
>>ERROR: column "testname" does not exist
>>LINE 1: ...SERT INTO pb_properties (c_name, c_value) VALUES (testname, ...
^
>>DB_QUERY: rows_count =
现在如何使用 db_query 函数执行成功的 INSERT 语句?我已经尝试过以多种方式屏蔽我尝试插入的值,但它们都不起作用。我想这与通过 SSH 运行 sql 命令和我使用的不同引号 `、" 和 ' 的组合有关。
I have the following script, which connects through ssh to a remote server and issues an SQL statement on it's Postgresql database:
#!/bin/bash
db_query() {
app_server="$1"
sql_stmt="$2"
psql_cmd="psql -d vdc --pset tuples_only -c '$sql_stmt'"
rows_count=`ssh $app_server "sudo su - postgres -c \"$psql_cmd\""`
echo "DB_QUERY: rows_count = $rows_count"
}
Now I'm trying to issue SELECT and INSERT statements to the following table:
CREATE TABLE pb_properties
(
c_name character varying(255) NOT NULL,
c_value character varying(255),
CONSTRAINT pb_properties_pkey PRIMARY KEY (c_name)
)
This function works fine if I do a SELECT statement:
#!/bin/bash
source db_query.sh
db_query staging "SELECT * FROM pb_properties;"
>> Output: DB_QUERY: rows_count = support-email | [email protected]
But it does not work if I do an INSERT statement:
#!/bin/bash
source db_query.sh
db_query prestaging "INSERT INTO pb_properties (c_name, c_value) VALUES ('support-email', '[email protected]');"
>> Output:
>>ERROR: column "testname" does not exist
>>LINE 1: ...SERT INTO pb_properties (c_name, c_value) VALUES (testname, ...
^
>>DB_QUERY: rows_count =
Now how can I do a successful INSERT statement with my db_query function? I already tried masking the values I tried to insert in many several ways, but none of them worked. I guess it has something to do with the combination of running the sql command through SSH and the different quotes i'm using `, " and '.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用以下代码示例来消除对 tmp 文件的依赖:
Here is a code sample you can use that removes the reliance of a tmp file:
好的,由于我无法使用问题中的 db_query 方法获取带单引号的 SQL 语句,因此我正在采取解决方法。在远程服务器上,我将 SQL 语句重定向到临时文件中。然后我可以让 psql 读取该文件,然后我就不必再关心引号了。
这是我的新 db_query 函数:
现在它适用于我最初的声明:
无论如何,如果有人提示如何在没有临时文件的情况下使其工作,我很高兴听到它。
Ok, since I cannot get the SQL statement with single quotes working at all with the db_query method in my question, I'm doing a workaround. On the remote Server, I'm redirecting the SQL statement into a temporary file. Then I can let psql read that file, then I don't have to care about quotes anymore.
This is my new db_query function:
This now works with my initial statement:
Anyway, if anybody has an hint how to get it working without a temporary file, I'd be glad to hear it.