【问题标题】:How to get tables from a database in cockroachdb with psycopg2?如何使用 psycopg2 从 cockroachdb 的数据库中获取表?
【发布时间】:2021-10-28 18:46:47
【问题描述】:

在 cockroachdb 中,如果我从 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 没有任何问题。所以我的问题是如何通过 cockorachdb 中的 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         |         | {}

【问题讨论】:

    标签: python psycopg2 cockroachdb


    【解决方案1】:

    想通了,问题是 psycog2 的 execute 没有返回任何东西,所以我也应该使用 fetchall

    cur.execute("show tables from _a66df261120b6c23")
    result = cur.fetchall()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-10
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 2011-11-06
      • 2020-06-14
      相关资源
      最近更新 更多