djhenderson / pypyodbc

Automatically exported from code.google.com/p/pypyodbc
0 stars 0 forks source link

Cannot access column names by attribute #55

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. pypyodbc.connect to SQL Server 2008
2. create cursor
3. execute select statement
4. try to access column by attribute name

What is the expected output? What do you see instead?
I expect to retrieve the columns data, instead, I get an AttributeError

What version of the product are you using? On what operating system?
Python 3.4.1 64-bit, on Windows 8.1 Pro, 64-bit, connecting to SQL Server 2008 
with "SQL Server" or "SQL Server Native Client 11.0" drivers

Please provide any additional information below.
Here's my console output:

>>> import pypyodbc
>>> dbconn = pypyodbc.connect('DRIVER={SQL 
Server};SERVER=psidevsql.paperless;DATABASE=NCOImport;UID=sa;PWD=d00rm0use')
>>> curs = dbconn.cursor()
>>> results = curs.execute('SELECT * FROM [Jobs]')
>>> jobrecord = results.fetchone()
>>> type(jobrecord) is None
False
>>> jobrecord.cursor_description
[('jobid', <class 'int'>, 20, 19, 19, 0, False), ('name', <class 'str'>, 50, 
50, 50, 0, False), ('description', <class 'str'>, 2147483647, 2147483647, 
2147483647, 0, True), ('srcdir', <class 'str'>, 2147483647, 2147483647, 
2147483647, 0, False), ('destdir', <class 'str'>, 2147483647, 2147483647, 
2147483647, 0, False), ('importeddir', <class 'str'>, 2147483647, 2147483647, 
2147483647, 0, False), ('isconverted', <class 'bool'>, 1, 1, 1, 0, True)]
>>> jobrecord.jobid
Traceback (most recent call last):
  File "<pyshell#48>", line 1, in <module>
    jobrecord.jobid
AttributeError: 'Row' object has no attribute 'jobid'
>>> jobrecord[0]
1
>>> 

Original issue reported on code.google.com by skillia...@gmail.com on 22 Oct 2014 at 3:40

Attachments:

GoogleCodeExporter commented 9 years ago
*Pay no attention to the production information there!*

My bad- any way I can get those specifics scrubbed post-submit?

Original comment by skillia...@gmail.com on 22 Oct 2014 at 3:44

GoogleCodeExporter commented 9 years ago
Hello?  Does anyone even use this?

Original comment by skillia...@gmail.com on 24 Oct 2014 at 11:59

GoogleCodeExporter commented 9 years ago
I think ou cannot. I usually build a dict(). See above:

            headers = [header[0] for header in cursor.description]
            rows = []
            for row in cursor:
                rows.append( dict( zip( headers, row ) ) )

Original comment by david.kwast on 9 Jan 2015 at 1:17

GoogleCodeExporter commented 9 years ago
Thanks, but I just made a helper class that caches namedtuple types for every 
table queried.  Now I can access fields by index and as named attributes (I 
have to rename any columns starting with numbers or "_" to "x_" which is pretty 
ugly, but eh...).

Original comment by skillia...@gmail.com on 9 Jan 2015 at 1:28