【问题标题】:PostgreSQL - how do you get the column formats?PostgreSQL - 你如何获得列格式?
【发布时间】:2015-01-08 03:08:04
【问题描述】:

我在 x86_64-unknown-linux-gnu 上使用 PostgreSQL 9.3.3,由 gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-52) 编译,64 位。

我已经完成了

psycopg2.connect

,得到光标,可以运行类似的代码行

cur.execute('SELECT latitude, longitude, date from db')
table = cur.fetchall()

据我了解http://initd.org/psycopg/docs/cursor.html,正在运行

print(cur.description)

应该显示每列的 type_code。我怎么没听懂?

我明白了

(Column(name='column_name', type_code=1043, display_size=None, internal_size=-1, precision=None, scale=None, null_ok=None), Column(name='data_type', type_code=1043, display_size=None, internal_size=-1, precision=None, scale=None, null_ok=None))

我被建议的另一个解决方案正在运行

cur.execute('select column_name, data_type from information_schema.columns')
cur.fetchall()
cols = cur.fetchall()

但这会返回一个空列表。

这就是我尝试过的。您对获取列格式有何建议?

【问题讨论】:

    标签: python postgresql psycopg2


    【解决方案1】:

    information_schema.columns 应该为您提供列数据类型信息。

    例如,给定这个 DDL:

    create table foo
    (
      id serial,
      name text,
      val int
    );
    
    
    insert into foo (name, val) values ('narf', 1), ('poit', 2);
    

    这个查询(过滤掉元表以获取您的表):

    select *
    from information_schema.columns
    where table_schema NOT IN ('information_schema', 'pg_catalog')
    order by table_schema, table_name;
    

    将产生 4 行,用于表 foo——我定义的三列,加上一个 FK。

    SQL fiddle

    关于psycopg2,您展示的与information_schema 相关的代码看起来应该可以工作......代码的全部内容是什么?我还建议尝试在调试器中单步执行代码(内置 pdb 可以,但我会推荐 pudb,因为它功能更全面且更易于使用,但仍然是终端-based。不过,由于它使用的底层模块,它只能在 *nix 平台上运行。

    编辑:

    我能够使用 psycopg2 和以下代码从 information_schema 获取 data_type 信息:

    #!/usr/bin/env python
    
    import psycopg2
    import psycopg2.extras
    
    conn = psycopg2.connect("host=<host> dbname=<dbname> user=<user> password=<password>")
    cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    
    cur.execute("""select *
                   from information_schema.columns
                   where table_schema NOT IN ('information_schema', 'pg_catalog')
                   order by table_schema, table_name""")
    
    for row in cur:
        print "schema: {schema}, table: {table}, column: {col}, type: {type}".format(
            schema = row['table_schema'], table = row['table_name'],
            col = row['column_name'], type = row['data_type'])
    

    我更喜欢使用DictCursors,因为我发现它们更容易使用,但它也应该使用常规游标——您只需要更改访问行的方式。

    另外,关于 cur.description,它返回 tupletuple。如果你想获得 type_code 那里,你可以这样做:

    print cur.description[0][1]
    

    您要查看的列的索引中的第一个维度,第二个维度是该列中的数据。 type_code 始终为 1。例如,您可以遍历外部元组并始终查看其第二项。

    【讨论】:

    • 您编辑的代码有效。鉴于此,并且我认为我已经发布了与使用“描述”相关的代码的必要部分,我不会发布代码。非常感谢!
    【解决方案2】:
    select oid,typname from pg_type;
    
     oid  |   typname
    ------+-------------
       16 | bool
       23 | int4
       25 | text
     1043 | varchar
     1184 | timestamptz 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-22
      • 2014-12-30
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      相关资源
      最近更新 更多