WestCoastProjects
WestCoastProjects

Reputation: 63192

Cursor description (columns list) is a 7-tuple even though only one column in the resultset

I am using the sqlite connector and examining the cursor returned from a query:

  curs = s.conn.cursor()
  curs.execute("select count(distinct category) as ncats from part")
  rows = curs.fetchall()

Examining the curs.description() in the debugger we see it is a 7-tuple of the single column ncats plus 6 Nones.

print(f'cursor description type: {type(curs.description)} 
        len: {len(curs.description[0])}')

cursor description type: <class 'tuple'> len: 7

Well the more the merrier ?? What are these for - and should we expect there to always be six extra participants in the results?

enter image description here

Upvotes: 1

Views: 844

Answers (1)

Shawn
Shawn

Reputation: 52549

From the documentation:

description

This read-only attribute provides the column names of the last query. To remain compatible with the Python DB API, it returns a 7-tuple for each column where the last six items of each tuple are None.

From the DB API documentation:

Each of these sequences contains information describing one result column:

  • name
  • type_code
  • display_size
  • internal_size
  • precision
  • scale
  • null_ok

The first two items (name and type_code) are mandatory, the other five are optional and are set to None if no meaningful values can be provided.

name is the only one of those that makes sense given sqlite's type system.

Upvotes: 1

Related Questions