platiumky / pymssql

Automatically exported from code.google.com/p/pymssql
GNU Lesser General Public License v2.1
0 stars 0 forks source link

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

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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:
<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()
</code>

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

Original issue reported on code.google.com by kalucki....@gmail.com on 25 Jun 2012 at 11:19

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
# 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

Original comment by oetelaar.automatisering on 19 Nov 2012 at 8:47

GoogleCodeExporter commented 8 years ago
I committed a fix for this issue (with a test included):

http://code.google.com/p/pymssql/source/detail?r=08ae783880dd37ec4c7b5600e5815b4
9f167bbb2

Does this solve the problem for you guys and let you remove your workarounds?

Original comment by msabr...@gmail.com on 10 Jan 2013 at 1:11

GoogleCodeExporter commented 8 years ago
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?

Original comment by msabr...@gmail.com on 3 Apr 2013 at 7:22

GoogleCodeExporter commented 8 years ago
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)}

Original comment by srujan.v...@gmail.com on 9 Apr 2013 at 9:44

GoogleCodeExporter commented 8 years ago
Thanks, Srujan for verifying!

Original comment by msabr...@gmail.com on 9 Apr 2013 at 10:17