我有以下sql,我想返回所选数据
insert into test(id,name) select * from tmp_tbl on conflict(id) do update set name = EXCLUDED.name;
with selected_data as (select * from tmp_tbl) select * from selected_data;
drop table if exists tmp_tbl;
这是Python代码:
with psycopg.connect(self.connect_str, autocommit=True) as conn:
with conn.cursor() as cur:
cur.execute(sql_full)
rows = cur.fetchall()
colnames = [desc[0] for desc in cur.description]
df_result = pd.DataFrame(rows, columns=colnames)
return df_result
这会产生错误:
File "/usr/local/lib/python3.12/site-packages/psycopg/cursor.py", line 223, in fetchall
self._check_result_for_fetch()
File "/usr/local/lib/python3.12/site-packages/psycopg/_cursor_base.py", line 588, in _check_result_for_fetch
raise e.ProgrammingError("the last operation didn't produce a result")
psycopg.ProgrammingError: the last operation didn't produce a result
我可以直接在 sql 客户端(dbweaver)中执行该 sql 并且它可以工作。但 python/psycopg 代码没有。 任何帮助表示赞赏
尝试在末尾添加另一个表达式:
SELECT 1 FROM TABLE_NAME