gpt4 book ai didi

sql - 如何从批处理文件执行 postgres 的 sql 查询?

转载 作者:行者123 更新时间:2023-11-29 11:19:39 26 4
gpt4 key购买 nike

我需要从批处理文件中执行 SQL。我正在执行以下连接到 Postgres 并从表中选择数据

C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME% 
select * from test;

我可以连接到数据库,但是我收到了错误

'select' is not recognized as an internal or external command, operable program or batch file.

有人遇到过这样的问题吗?

这是我正在尝试的查询之一,类似的东西在 shell 脚本中有效,(如果有的话,请忽略查询中的语法错误)

copy testdata (col1,col2,col3) from '%filepath%/%csv_file%' with csv;

最佳答案

你可以将它通过管道传递给 psql

(
echo select * from test;
) | C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%

当右括号是 SQL 查询的一部分时,它们必须使用三个脱字符进行转义。

( 
echo insert into testconfig(testid,scenarioid,testname ^^^) values( 1,1,'asdf'^^^);
) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%

关于sql - 如何从批处理文件执行 postgres 的 sql 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34880453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com