【问题标题】:getting exact sql type of query's result rowset in python在python中获取查询结果行集的确切sql类型
【发布时间】:2012-07-02 10:51:27
【问题描述】:

据我所知,标准 Python DB api 不提供结果行集的确切 SQL 类型信息。

对于某些结果行集,是否可以获得确切的 SQL 类型和一些更详细的信息,如 tinyint、bigint、nvarchar(100),而不是像 int、str ... 这样的截断 python 类型?

我认为可移植性或模块依赖性并不重要——只要能正常工作就可以了。 (我正在研究 Windows,MS-SQL。)

【问题讨论】:

    标签: python sql sql-server database


    【解决方案1】:

    在查询后使用.description 来检索有关您的数据类型等信息。

    cursor.execute('select some_column from some_table')
    print cursor.description
    

    .description

            This read-only attribute is a sequence of 7-item
            sequences.  
    
            Each of these sequences contains information describing
            one result column: 
    
              (name, 
               type_code, 
               display_size,
               internal_size, 
               precision, 
               scale, 
               null_ok)
    

    【讨论】:

    • 感谢您的回答,尽管游标对象中的“描述”成员并未提供我所知道的确切 SQL 类型信息。 (例如,没有办法区分xml/nvarchar/varchar ...)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 2023-03-25
    • 2020-01-15
    • 2013-03-25
    • 1970-01-01
    • 2017-03-11
    相关资源
    最近更新 更多