使用管道将函数生成的 SQL 查询重定向到 psql 命令
我有一个脚本,可以生成 SQL 查询作为文本,例如,
...
return "SELECT COUNT(*) FROM important_table"
然后我想在 PostgreSQL 数据库上运行它。我可以用这样的两行来完成此操作:
python SQLgeneratingScript.py parameters > temp.sql
psql -f temp.sql -d my_database
但似乎我应该能够用管道将其排成一行,但我不知道如何操作。
I have a script that generates an SQL query as text e.g.
...
return "SELECT COUNT(*) FROM important_table"
which I would then like to run on a PostgreSQL database. I can do this in two lines like this:
python SQLgeneratingScript.py parameters > temp.sql
psql -f temp.sql -d my_database
But seems like I should be able to one-line it with pipes, but I don't know how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你为什么不使用像 psycopg2 这样的 python postgresql 连接器,但是如果你想这样做你想要使用Unix命令重定向做什么,你必须在你的代码中这样做,
如果你运行了你的命令,这将写入你的文件
temp.sql
像这样:但是我会建议像这样在Python文件中写入
,或者更多使用
psycopg2
直接执行你的sqli don't know why you are not using a python postgresql connector like psycopg2 ,but well if you want to do what you are trying to do using Unix command redirection you will have to do it like this in your code
this will write in your file
temp.sql
if you have ran your command like this:but well i will suggest writing in you file in python like this
or more use
psycopg2
to execute directly your sql