0

I have connected to a db and have a cursor. I wish to get the column name and data type of each column.

My code is this:

for row in cursor.columns(table='my_table'):
  print(row)
  print(type(row))

Output is this:

('data', 'dbo', 'my_table', 'system_code', -9, 'nvarchar', 10, 20, None, None, 0, None, None, -9, None, 20, 1, 'NO', 0, 0, 0, 0, None, None, None, None, None, None, 39)
<class 'pyodbc.Row'>

I can find no documentaion on what these values represent. I can hower get the column name by running:

for row in cursor.columns(table='my_table'):
    print row.column_name

So column_name is an attribute- what are the other attribute names? Are these documented somewhere?

Preston
  • 4,628
  • 3
  • 33
  • 58

1 Answers1

1

The pyodbc Cursor#columns method is documented in the pyodbc wiki:

Each row has the following columns:

 1. table_cat
 2. table_schem
 3. table_name
 4. column_name
 5. data_type
 6. type_name
 7. column_size
 8. buffer_length
 9. decimal_digits
10. num_prec_radix
11. nullable
12. remarks
13. column_def
14. sql_data_type
15. sql_datetime_sub
16. char_octet_length
17. ordinal_position
18. is_nullable: One of SQL_NULLABLE, SQL_NO_NULLS, SQL_NULLS_UNKNOWN.

https://github.com/mkleehammer/pyodbc/wiki/Cursor#columnstablenone-catalognone-schemanone-columnnone

Gord Thompson
  • 98,607
  • 26
  • 164
  • 342