0

I am trying to get the mssql table column name and datatype using pyodbc

Here is my code:

def GetSQLTableFields(connDetails):
    cursor = conn.ConnectSQL(connDetails)
    cursor.execute("SELECT colum1, column2, column3 FROM TEST_TABLE")
    for col in cursor.description:
        print('name =', col[0], ', datatype =',col[1])
    cursor.close()

by using this code i'm not able to get the sql datatype of columns inseted i'm getting this

name = column1, datatype = <class 'str'>
name = column2, datatype = <class 'int'>

output i want

name = column1, datatype = nvarchar
name = column2, datatype = int
Vinayak
  • 109
  • 2
  • 12
  • Does this answer your question? [Get MSSQL table column names using pyodbc in python](https://stackoverflow.com/questions/34678006/get-mssql-table-column-names-using-pyodbc-in-python) – Amira Bedhiafi May 18 '20 at 11:38
  • No in the given example there is no datatype retrieval, and in my scenario the input is a sql query with mentioned column names not just a tableName – Vinayak May 18 '20 at 11:59

1 Answers1

2

This is a long-standing feature request that has not been implemented in pyodbc itself.

https://github.com/mkleehammer/pyodbc/issues/167

You may want to try using the fork mentioned in that issue (if it is being maintained).

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418