【发布时间】:2017-02-05 20:46:30
【问题描述】:
我正在使用 python psycopg2 库在 postgres 数据库中创建一个表:
self.conn=pg.connect(host='localhost',user='eba',password='****',database='eba')
cur=self.conn.cursor()
cur.execute(sql)
cur.close()
sql='''CREATE TABLE public.tempimport (
id integer NOT NULL DEFAULT nextval('tempimport_id_seq'::regclass),
tablename character varying(32) COLLATE pg_catalog."default",
index_ character varying(32) COLLATE pg_catalog."default",
CONSTRAINT tempimport_pkey PRIMARY KEY (id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.tempimport
OWNER to eba;'''
cur=self.conn.cursor()
cur.execute(sql)
cur.close()
之后,如果我运行:
cur=self.conn.cursor()
cur.execute("SELECT count(*) FROM pg_catalog.pg_tables where tablename = 'tempimport'")
x=cur.fetchall()
print x
我得到1作为答案,即表存在。
但是,如果我使用 pgAdmin 登录相同的数据库/用户/密码并运行相同的 SELECT COUNT(*)... 语句,我会得到 0 作为答案。
我用代码创建的表在哪里?
我怎样才能找到它在哪里?
【问题讨论】:
-
也许你也需要
commit。 -
谢谢,就是这样!
-
。 .得到我的次数比我想承认的要多。
标签: python postgresql