pypyodbc
pypyodbc copied to clipboard
All-caps columns can't be found
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.
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
YoU SaVeD mY DaY. ✌️