Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT results with wrong column order with PyMySQL

Tags:

python

mysql

I'm executing a SQL "SELECT" query on a MySQL database via python, using PyMySQL as the interface. Below is the excerpt of the code which performs the task:

try:
        with self.connection.cursor() as cursor:
            sql = "SELECT `symbol`,`clordid`,`side`,`status` FROM " + tablename + " WHERE `tradedate` >= %s AND (`status` =%s OR `status`=%s)"
            cursor.execute(sql,(str(begindate.date()),'I','T'))
            a = cursor.fetchall()

The query executes just fine. The problem is that the column ordering of the results doesn't match the order specified within the query. If I run add the following code:

for b in a:
            print b.values()

The values in variable 'b' appear in the following order:

'status', 'symbol', 'side', 'clordid'

Moreover, it doesn't matter which order is specified by me- the results always appear in this order. Is there any way to fix this? Thanks in advance!

like image 757
bsam Avatar asked Sep 10 '15 13:09

bsam


People also ask

Does GROUP BY column order matter?

No, the order doesn't matter for the GROUP BY clause. MySQL and SQLite are the only databases I'm aware of that allow you to select columns which are omitted from the group by (non-standard, not portable) but the order doesn't matter there either.

Does MySQL column order matter?

Yes, column order does matter.

How would you access a specific column value from a select query using Fetchone?

Steps for using fetchone() in Mysql using Python: Next, create a cursor object with the cursor() method. Now create and execute the query using “SELECT *” statement with execute() method to retrieve the data. Use fetchone() method on the result variable. print the result.

What is Pymysql cursors DictCursor?

class pymysql.cursors. DictCursor (connection) A cursor which returns results as a dictionary. class pymysql.cursors.


1 Answers

In testing I found the selected answer (convert dict to OrderedDict) to be unreliable in preserving query result column order.

@vaultah's answer in a similar question suggests using pymysql.cursors.DictCursorMixin:

class OrderedDictCursor(DictCursorMixin, Cursor):
    dict_type = OrderedDict

...to create a cursor that remembers the correct column order:

cursor = conn.cursor(OrderedDictCursor)

Then get your results like normal:

results = cursor.fetchall()
for row in results:
    print row # properly ordered columns

I prefer this approach better because it's stable, requires less code, and handles ordering at the appropriate level (as the columns are read).

like image 68
jbryanscott Avatar answered Sep 20 '22 20:09

jbryanscott