Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing row values in pyodbc when column name contains dashes (hyphens)

I am new to python and pyodbc

I try to print the first a row from a table from a progress openedge database. (Windows 7) Here is the code block that is not running:

cursor.execute("select my-nr, my-dt-my from mytable")
row = cursor.fetchone()
print(row.my-nr, row.my-dt-my)

This gives errors undefined name: 'nr' undefined name 'dt' undefined name 'my'

I guess it has something to do with the minus - symbols behind the dot . in print(row.my-nr, row.my-dt-my)

It was easy to print out the table names and column names from the database earlier but for some reason printing out rows is harder.

Any ideas how to get the rows printed?

like image 957
BPH Avatar asked Nov 23 '17 11:11

BPH


People also ask

How to reference values in a pyodbc row?

pyodbc allows us to reference values in a pyodbc.Row object using the form row.column_name provided that the column names are legal Python identifiers. So, for example, we can do something like to print the value of the "city" column.

How to select rows&columns by name or index in pandas?

Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc. Indexing in Pandas means selecting rows and columns of data from a Dataframe. It can be selecting all the rows and the particular number of columns, a particular number of rows, and all the columns or a particular number of rows and columns each.

How do I quote a column with a hyphen in Python?

First, columns containing hyphens need to be quoted in OpenEdge ( see here ). Second, you can alias the columns so they can be referenced as valid Python attributes. You'll need to do something like this: cursor.execute ('select "my-nr" as mynr, "my-dt-my" as mydtmy from mytable') row = cursor.fetchone () print (row.mynr, row.mydtmy)

How to fetch all rows from a database table in Python?

To fetch all rows from a database table, you need to follow these simple steps: – Create a database Connection from Python. Define the SELECT query. Here you need to know the table, and it’s column details. Execute the SELECT query using the cursor.execute () method. Get resultSet (all rows) from the cursor object using a cursor.fetchall ().


2 Answers

pyodbc allows us to reference values in a pyodbc.Row object using the form row.column_name provided that the column names are legal Python identifiers. So, for example, we can do something like

row = crsr.fetchone()
print(row.city)

to print the value of the "city" column. Unfortunately, my-nr is not a legal Python identifier so if we try to print the value of the "my-nr" column using ...

row = crsr.fetchone()
print(row.my-nr)  # error

... Python parses that as "row.my minus nr" where row.my would be interpreted as a column in the Row object and nr would be interpreted as a Python variable.

To work around the issue we can grab a list of the column names, merge those names with the row values into a dictionary, and then refer to the values in the dictionary:

crsr.execute(sql)
col_names = [x[0] for x in crsr.description]

row = crsr.fetchone()
row_as_dict = dict(zip(col_names, row))
print(row_as_dict['my-nr'])  # no error
like image 155
Gord Thompson Avatar answered Oct 20 '22 17:10

Gord Thompson


The most simple solution I can think of is this. First, columns containing hyphens need to be quoted in OpenEdge (see here). Second, you can alias the columns so they can be referenced as valid Python attributes. You'll need to do something like this:

cursor.execute('select "my-nr" as mynr, "my-dt-my" as mydtmy from mytable')
row = cursor.fetchone()
print(row.mynr, row.mydtmy)

Good luck!

like image 43
FlipperPA Avatar answered Oct 20 '22 16:10

FlipperPA