2024-06-02 07:00:38 发布
网友
这对我有好处:
cursor.execute("""SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'""") for table in cursor.fetchall(): print(table)
问题是使用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_names
pg_类存储所有必需的信息。
执行下面的查询将返回用户定义的表作为列表中的元组
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',)]
这对我有好处:
问题是使用python的psycopg2来处理postgres。这里有两个方便的功能:
pg_类存储所有必需的信息。
执行下面的查询将返回用户定义的表作为列表中的元组
输出:
相关问题 更多 >
编程相关推荐