Result type from cursor.fetchall() is list of tuples, but doc shows tuple of tuples

2k Views Asked by At
from django.db import connection

conn = connection.cursor()
conn.execute("some select query..")
print( conn.fetchall() )

This shows that result from cursor.fetchall() is list of tuples, though in docs there is example:

>>> cursor.execute("SELECT id, parent_id FROM test LIMIT 2");
>>> cursor.fetchall()
((54360982, None), (54360880, None))

That shows that result is not list, but tuple of tuples.

Little bit confused, What I'm missing here?

Thanks

1

There are 1 best solutions below

0
questionto42 On

(54360982, None) is a tuple. And ((54360982, None), (54360880, None)) is a tuple of tuples, which is like in the docs. That is why the return type is also the builtin tuple, checked with mySQL and sqlalchemy which again uses MySQLdb for the cursor:

import sqlalchemy
# from MySQLdb.cursors import Cursor --> this would be the cursor type if we needed it

print(type(cursor.fetchall()))

Out:

<class 'tuple'>

Your remark

This shows that result from cursor.fetchall() is list of tuples

seems to say that from django.db import connection leads to an output:

<class 'list'>

At least if I understand you right. If that is true, I do not understand it either, I can only say from the test with sqlalchemy that this looks like a mere django problem, as already said in the comments.