Can I use pyodbc/freetds and sqlalchemy with decimal data in mssql?

痞子三分冷 提交于 2019-12-10 22:58:36

问题


This has been a long running issue for me. I have a proprietary database that I cannot change and many of the tables have fields which are defined as e.g. decimal(12, 4).

When I try to pull data from such a table on ubuntu 12.04 using pyodbc/freeTDS like this...

import pyodbc
connection_string = 'DRIVER={FreeTDS};DSN=<myDSN>;UID=<my_user>;PWD=<my_password>;'
conn = pyodbc.connect(connection_string)
cur = conn.cursor()
cur.execute('SELECT myfield FROM mytable')
for row in cur.fetchall():
    print row[0]

...I get a really unhelpful message.

Traceback (most recent call last):   File
"/path/to/testing_pyodbc.py", line 6, in <module>
    for row in cur.fetchall(): pyodbc.Error: ('HY000', 'The driver did not supply an error!')

Whereas if I cast the result to a float the query runs with no problem.

import pyodbc
connection_string = 'DRIVER={FreeTDS};DSN=<myDSN>;UID=<my_user>;PWD=<my_password>;'
conn = pyodbc.connect(connection_string)
cur = conn.cursor()
cur.execute('SELECT CAST(myfield AS FLOAT) FROM mytable')
for row in cur.fetchall():
    print row[0]

My first question is can I fix this problem without changing the table structure? The database is not mine so I have no access to change it.

I would like to use SQLAlachemy to grab this data from the database. I am doing so happily on Windows like this.

class MyTable(Base):
    __tablename__ = u'table'
    ...
    myfield = Column(DECIMAL(12, 4), nullable=True)
    another_field = Column(DECIMAL(12, 4), nullable=True)
    ...

My second question (if the first is not solvable) is can I define my sqlAlchemy class to automatically cast the data to a float under the hood so the code that uses the class needn't worry about it?

I am running ubuntu 12.04 so the installed version of freetds is 0.91:

$ dpkg -s freetds-common
Package: freetds-common
Status: install ok installed
Multi-Arch: foreign
Priority: optional
Section: libs
Installed-Size: 91
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: all
Source: freetds
Version: 0.91-1
Replaces: libct3, libct4 (<< 0.82-1)
Description: configuration files for FreeTDS SQL client libraries
 FreeTDS is an implementation of the Tabular DataStream protocol, used for
 connecting to MS SQL and Sybase servers over TCP/IP.
 .
 This package manages the configuration files that are common to all of
 the TDS client library implementations (CT-Lib, DB-Lib, and ODBC),
 stored in /etc/freetds/.
Original-Maintainer: Steve Langasek <vorlon@debian.org>
Homepage: http://www.freetds.org/

But when I ask tsql, it tells me v0.64:

$ tsql -C
Compile-time settings (established with the "configure" script):
                           Version: freetds v0.64
    MS db-lib source compatibility: no
       Sybase binary compatibility: unknown
                     Thread safety: yes
                     iconv library: yes
                       TDS version: 5.0
                             iODBC: no
                          unixodbc: yes

Also note that when I use tsql or isql on the command line, they are happy to give me the data without the CAST() operation.

来源:https://stackoverflow.com/questions/18785670/can-i-use-pyodbc-freetds-and-sqlalchemy-with-decimal-data-in-mssql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!