Loprz / pypyodbc

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

support field name as cursor attribute #10

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.no support field name as cursor attribute 

Please provide any additional information below.

i managed to get this support but it's only apply for fetchall()
cause i only use this function when retrieving data

Original issue reported on code.google.com by BL3A...@gmail.com on 26 Mar 2013 at 10:15

Attachments:

GoogleCodeExporter commented 8 years ago
I tend to not have this feature, because it's not compliant to dbi 2.0 standard.
Also, I feel it will have problems supporting below cases:

1) Field name containing spaces. For example a field "A field", you can not 
access it by cur.A field

2) Non ascii name, For example cur.姓名

Original comment by jiangwen...@gmail.com on 26 Mar 2013 at 2:16

GoogleCodeExporter commented 8 years ago
I think it will be very helpful to support this option even if it's not 
compliant with dbi v2.0 at least make it an option user's can set and not 
default

and for those cases we can improve it by force 'utf-8' and make second level 
that return a dict instead of cursor attribute
something like :
1- cursor.SetRetriveLevel=1 #this is default row[0]
2- cursor.SetRetriveLevel=2 #add field name as cursor attribute row.field
3- cursor.SetRetriveLevel=3 #add field name as cursor dict row["A field"]

thanks :)

Original comment by BL3A...@gmail.com on 27 Mar 2013 at 12:18

GoogleCodeExporter commented 8 years ago
Yes, I have actually been thinking about adding a method to the cursor object 
or the row object, something like:
cur.get("A field") or cur.get("姓名")

However, there's one existing hiding feature of the module worth to mention:

You can change one line to enable "row.A_field" feature:

In the method Cursor.__init__(), find the line:
        self.row_type_callable = row_type_callable or TupleRow
Change TupleRow to NamedTupleRow:
        self.row_type_callable = row_type_callable or NamedTupleRow

This would enable the row.A_field feature, but would fail in the "A field" and 
non-ascii case, so it was not enabled by default, and I would still prefer to 
create a certain function like cur.get("A field")

Hope that helps and thanks for your suggesting. Stay turned.

Original comment by jiangwen...@gmail.com on 27 Mar 2013 at 12:47

GoogleCodeExporter commented 8 years ago

Original comment by jiangwen...@gmail.com on 29 Mar 2013 at 7:02

GoogleCodeExporter commented 8 years ago
Added a get method for Row object, now you can do row.get("A field")

Original comment by jiangwen...@gmail.com on 3 Apr 2013 at 11:59

GoogleCodeExporter commented 8 years ago
Just uploaded 1.1.1, now you can get a field's value of a row by: row['field 
name']

Original comment by jiangwen...@gmail.com on 4 Apr 2013 at 3:11

GoogleCodeExporter commented 8 years ago
I suggest this is added as a caveat on the main page, beside "almost can be 
seen like a drop-in replacement of pyodbc in pure Python".

Original comment by cyborg10...@gmail.com on 27 Nov 2013 at 10:13

GoogleCodeExporter commented 8 years ago
This does not seem to work.
I am connecting to a SQL Server 2008 database via the FreeTDS odbc driver. 
For example, my query:
  SELECT rts.ID as ID, rts.DateTimeStamp as DateTimeStamp FROM....
Then
    results = sql_server_db.execute(self, sql, station)
    if results:
      date_list = []
      for row in results: 
        date_list.append((row['ID'], row['DateTimeStamp']))

My list is full of tuples of (None,None), although the data is there if I 
access via (row[0], row[1])

Original comment by d...@inlet.geol.sc.edu on 6 Jun 2014 at 6:15

GoogleCodeExporter commented 8 years ago
Same experience as above. Using MSSQL via freetds row['anything'] & .get is 
returning None

Original comment by Gam...@googlemail.com on 9 Jun 2014 at 10:32

GoogleCodeExporter commented 8 years ago
#8, #9 - your problems might be related to 
https://code.google.com/p/pypyodbc/issues/detail?id=40.

Original comment by alex.s.y...@gmail.com on 25 Jun 2014 at 9:35