mkleehammer / pyodbc

Python ODBC bridge
https://github.com/mkleehammer/pyodbc/wiki
MIT No Attribution
2.88k stars 562 forks source link

[ODBC][MSSQL] Missing precision scale #1111

Closed Platob closed 1 year ago

Platob commented 1 year ago

Environment

Issue

I cannot recover the precision and scale in cursor.description, is it possible to add it ?

v-chojas commented 1 year ago

Could you explain in a little more detail what you're asking for? Does the cursor.description https://github.com/mkleehammer/pyodbc/wiki/Cursor#description not work for you?

Platob commented 1 year ago

At the moment i can recover data types like this, but i would need more details like precision, scale and more precise matching between data types and type code

('int', 3, None, None, None, None, None)
('bigint', 3, None, None, None, None, None)
('bit', 3, None, None, None, None, None)
('decimal', 5, None, None, None, None, None)
('float', 3, None, None, None, None, None)
('real', 3, None, None, None, None, None)
('date', 2, None, None, None, None, None)
('datetime', 4, None, None, None, None, None)
('datetime2', 2, None, None, None, None, None)
('smalldatetime', 4, None, None, None, None, None)
('time', 2, None, None, None, None, None)
('string', 1, None, None, None, None, None)
('binary', 2, None, None, None, None, None)
v-chojas commented 1 year ago

According to the documentation, the description field should contain the precision and scale. Why that isn't happening needs to be determined, so can you post an ODBC trace of the scenario? According to the source https://github.com/mkleehammer/pyodbc/blob/master/src/cursor.cpp#L266 the precision and scale fields should be coming from a SQLDescribeCol call, so the fact that they show up as None looks like a bug somewhere.

gordthompson commented 1 year ago

FWIW, both Driver 17 and Driver 18 on Windows seem to work against a SQL Server 2019 instance:

import pyodbc

print(pyodbc.version)  # 4.0.34
cnxn = pyodbc.connect(
    "DRIVER=ODBC Driver 18 for SQL Server;"
    "SERVER=192.168.0.199;"
    "UID=scott;PWD=tiger^5HHH;"
    "DATABASE=test;"
    "TrustServerCertificate=yes;"
)
crsr = cnxn.cursor()
crsr.execute("SELECT CAST(3.14 AS decimal(18, 4)) AS pi")
print(crsr.description)
# (('pi', <class 'decimal.Decimal'>, None, 18, 18, 4, True),)
Platob commented 1 year ago

Was using native sql server, not like @gordthompson This one works thanks