在run中,如果我从SQL终端中运行SHOW DATABASES;,我会得到:
database_name | owner | primary_region | regions | survival_goal
--------------------+-------+----------------+---------+----------------
_a66df261120b6c23 | root | NULL | {} | NULL
defaultdb | root | NULL | {} | NULL
postgres | root | NULL | {} | NULL
sammy | root | NULL | {} | NULL
system | node | NULL | {} | NULL
test123 | root | NULL | {} | NULL
test3 | root | NULL | {} | NULL
test4 | root | NULL | {} | NULL
test5 | root | NULL | {} | NULL
test9 | root | NULL | {} | NULL
(10 rows)如果我现在运行SHOW TABLES FROM _a66df261120b6c23,我会得到:
schema_name | table_name | type | owner | estimated_row_count | locality
--------------+-------------------+-------+-------+---------------------+-----------
public | __Auth | table | root | 0 | NULL
public | tabDefaultValue | table | root | 0 | NULL
public | tabDocType Action | table | root | 0 | NULL
public | tabDocType Link | table | root | 0 | NULL
public | tabFile | table | root | 0 | NULL
public | tabSeries | table | root | 0 | NULL
public | tabSessions | table | root | 0 | NULL
public | tabSingles | table | root | 0 | NULL
public | tabdocfield | table | root | 0 | NULL
public | tabdocperm | table | root | 0 | NULL
public | tabdoctype | table | root | 0 | NULL问题是,当我试图从psycopg2执行相同的查询时,例如:SHOW TABLES FROM _a66df261120b6c23,我得到了错误:
test1 = cur.execute("show tables from _a66df261120b6c23;")
psycopg2.errors.InvalidCatalogName: database "root" does not exist以下是python脚本:
with rdb_conn.cursor() as cur:
# cur.execute('CREATE database test9;')
test1 = cur.execute("show tables from _a66df261120b6c23;")
# print("test1: ", test1)
# result = cur.execute("CREATE DATABASE test9;")
rdb_conn.commit()我应该提一下,创建数据库不是问题,例如,CREATE DATABASE test9就没有任何问题。所以我的问题是,如何通过psycopg2获取给定数据库中的表?
cursor.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")对我不起作用,_a66df261120b6c23中的表都不显示
以下是用户列表:
username | options | member_of
--------------------+---------+------------
_a66df261120b6c23 | | {}
admin | | {}
doug | | {admin}
postgres | | {}
root | | {admin}
sammy | | {admin}
sammy2 | | {admin}
sammy3 | | {}
test_user | | {}发布于 2021-08-29 22:00:28
弄清楚了,问题是fetchall 2的execute没有返回任何东西,所以我也应该使用它。
cur.execute("show tables from _a66df261120b6c23")
result = cur.fetchall()https://stackoverflow.com/questions/68976941
复制相似问题