有人能解释一下如何在当前数据库中获取这些表吗?
我使用的是postgresql-8.4 psycopg2。
发布于 2014-06-28 09:39:04
这为我做了这个把戏:
cursor.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")
for table in cursor.fetchall():
print(table)发布于 2014-06-03 14:58:12
pg_class存储所有必需的信息。
执行以下查询将以列表中的元组形式返回用户定义的表
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
print cursor.fetchall()输出:
[('table1',), ('table2',), ('table3',)]发布于 2014-02-01 01:02:59
问题是如何使用python的psycopg2来处理postgres。这里有两个方便的函数:
def table_exists(con, table_str):
exists = False
try:
cur = con.cursor()
cur.execute("select exists(select relname from pg_class where relname='" + table_str + "')")
exists = cur.fetchone()[0]
print exists
cur.close()
except psycopg2.Error as e:
print e
return exists
def get_table_col_names(con, table_str):
col_names = []
try:
cur = con.cursor()
cur.execute("select * from " + table_str + " LIMIT 0")
for desc in cur.description:
col_names.append(desc[0])
cur.close()
except psycopg2.Error as e:
print e
return col_nameshttps://stackoverflow.com/questions/10598002
复制相似问题