clueboy / pymssql_issues

0 stars 0 forks source link

Cursor fetch* methods return redundant keys for column names and column numbers. #92

Closed clueboy closed 11 years ago

clueboy commented 11 years ago

From kalucki.jacek on June 25, 2012 04:19:03

For dictionary cursors, cursor fetch* methods return redundant keys for both column names and column numbers.

Package version: 2.0.0b1 Python version: 2.7.3 Platform: Windows 32

Steps to reproduce problem, execute code: import pymssql conn = pymssql.connect(host='localhost', user='sa', password='secret') cur = conn.cursor(as_dict=True) cur.execute('SELECT name FROM sys.sysdatabases') cur.fetchone()

Expected output is: {'name': u'master'} but I receive {0: u'master', 'name': u'master'} instead.

Original issue: http://code.google.com/p/pymssql/issues/detail?id=92

clueboy commented 11 years ago

From oetelaar.automatisering on November 19, 2012 12:47:06

I confirm this problem.

this is how I solve it now

row=cur.fetchone()

clean_dict(row)

def clean_dict(mydict): """clean a database row remove numeric keys, keep the named keys modifies original dict in place, be careful """

remove numeric keys

for i in range(len(mydict)):
    try:
        del mydict[i]
    except KeyError, e:
        break
return mydict
clueboy commented 11 years ago

From msabramo on January 09, 2013 17:11:10

I committed a fix for this issue (with a test included): http://code.google.com/p/pymssql/source/detail?r=08ae783880dd37ec4c7b5600e5815b49f167bbb2 Does this solve the problem for you guys and let you remove your workarounds?

Status: Fixed
Owner: msabramo

clueboy commented 11 years ago

From msabramo on April 03, 2013 12:22:27

pymssql-2.0.0b1-dev-20130403.tar.gz at https://code.google.com/p/pymssql/downloads/list has this change.

Can someone other than me confirm that it works?

clueboy commented 11 years ago

From srujan.vanama on April 09, 2013 14:44:36

Fix looks good, I installed the latest version of pymssql

(pymssql_test)srujanv-mac:test srujanv$ pip freeze | grep pymssql Warning: cannot find svn location for pymssql==2.0.0b1-dev-20130403 pymssql==2.0.0b1-dev-20130403

Executed the following query with 'as_dict=True'

Query:

SELECT [ID] ,[EmployeeID] ,[FirstName] ,[LastName] ,[JoiningDate] ,[AreaCode] FROM [Employee]

Result-set returned

{'EmployeeID': 'E10021 ', 'FirstName': 'John', 'AreaCode': 'CA001 ', 'LastName': 'Doe', 'ID': 1L, 'JoiningDate': datetime.datetime(2013, 1, 1, 0, 12, 12, 123000)} {'EmployeeID': 'E10032 ', 'FirstName': 'Oliver', 'AreaCode': 'CA000 ', 'LastName': 'Blume', 'ID': 2L, 'JoiningDate': datetime.datetime(2012, 1, 1, 8, 0, 0, 123000)} {'EmployeeID': 'E11033 ', 'FirstName': 'Jonathan', 'AreaCode': 'CA001 ', 'LastName': 'J', 'ID': 21L, 'JoiningDate': datetime.datetime(2012, 12, 1, 12, 12, 12)}

clueboy commented 11 years ago

From msabramo on April 09, 2013 15:17:07

Thanks, Srujan for verifying!

Status: Verified