【发布时间】:2020-09-01 01:29:24
【问题描述】:
我正在尝试在 postgres 中创建一个临时表。我正在使用 psycopg2。但是,在执行我的 python 脚本时,我没有看到正在创建的表。如果我尝试创建一个普通表,它会毫无问题地执行。有什么我遗漏的概念吗?我的目的是创建一个临时表,然后将数据导入临时表,进行一些处理,然后将临时表数据推送到永久表中。
下面是我的代码
import psycopg2
con = None
con = psycopg2.connect("dbname='abc' user='abc' host='localhost' password='abc'")
cur = con.cursor()
cur.execute("create TEMP TABLE mytable(id int);")
con.commit()
con.close()
如果我使用终端执行相同的create TEMP TABLE mytable(id int);,它会创建临时表。
感谢@Thom Brown 的回复。
我确实想到了你的观点。为了绕过 con.close() 的问题,我做了以下,但没有成功。
con = None
con = psycopg2.connect("dbname='abc' user='abc' host='localhost' password='abc'")
cur = con.cursor()
cur.execute("create temp table mynewtable(id int) on commit delete rows;")
cur.execute("BEGIN TRANSACTION;")
cur.execute("insert into mynewtable values(10);")
//created a new normal table from console
cur.execute("select * into dbo.test from mynewtable;")
#cur.execute("select * into test from mynewtable;")
cur.execute("commit")
con.commit()
con.close()
理论上,我应该能够使用执行命令创建临时表,向其中插入一些数据,将所有数据从临时表传输到永久表,关闭连接。临时表数据将丢失,但永久表应该有传输的数据。这没有发生。请指导。
【问题讨论】:
标签: mysql postgresql psycopg2 psql