pypyodbc icon indicating copy to clipboard operation
pypyodbc copied to clipboard

All-caps columns can't be found

Open qguv opened this issue 9 years ago • 2 comments

When working with a MS SQL 2008 database through ODBC, all-caps column names in a row must be accessed in lowercase to yield the correct information.

for row in cur.execute("select * from TOTAL_DETAIL where IDENTIFIER = ?", (id, )):
    print(row["PRODUCT"]) # None
    print(row["product"]) # the correct product name

If this is a quirk of ODBC, a possible solution (if it doesn't cause other problems) would be to .lower() all keys before searching the Row-internal dictionary.

qguv avatar May 29 '15 19:05 qguv

Hi, dict keys are case sensitive natively in language... not as the column names / table names / db names in sql server... to achieve a pseudo-case-insensitiviness lib has done .lower() to all names.

import pypyodbc; pypyodbc.lowercase = True # (Default behaviour)

In that way you will have to specify everywhere (and get used to) use lowercased names everywhere in your app (recommended)

import pypyodbc; pypyodbc.lowercase = False

you will have to write names using the ExAcT case in names.

Will be a good idea to replace the native use of dict in lib to use a case-insensitive dict, like this one: https://gist.github.com/babakness/3901174 but there are others available also ...

https://www.python.org/dev/peps/pep-0455/ http://stackoverflow.com/a/2082169 http://stackoverflow.com/a/3296782

braian87b avatar Jan 17 '16 23:01 braian87b

YoU SaVeD mY DaY. ✌️

VishalK-NewcrestImage avatar Feb 06 '20 15:02 VishalK-NewcrestImage