gpt4 book ai didi

python - Postgres 仅在表存在时选择

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

如何执行

select * from my_table

但如果 my_table 不存在则避免抛出错误

这可能吗?

更新:我在 Odoo 中工作,当用户按下按钮时我创建了表格,所以第一次安装我的模块时表格将不存在,我不想看到错误。

我用 python 解决了这个问题:

try: cr.execute(my_query)
expetion: pass

如果我不够明确,我很抱歉,这是我在这里的第一个问题:P

最佳答案

您是否在某种功能中运行它?如果不是,整个问题就没有意义......如果是 - 只需 try catch 错误,如下所述:http://www.postgresql.org/docs/9.1/static/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING

您需要捕获 42P01 undefined_table SQLSTATE。

所以在你的函数中你可以这样做:

BEGIN
SELECT * FROM sometable
EXCEPTION
WHEN undefined_table THEN
-- Do something
WHEN others THEN
-- Do something else
END;

关于python - Postgres 仅在表存在时选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865553/

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