python - Pandas error when creating DataFrame from MS SQL Server database: 'ODBC SQL type -151 is not yet supported -


i'm trying create dataframe table in ms sql server 2016, have used sample database adventureworks2012, , here code:

import pyodbc  cnxn = pyodbc.connect("driver={odbc driver 13 sql server};"                       "server=localhost;"                       "database=adventureworks2012;"                       "trusted_connection=yes;")   cursor = cnxn.cursor() cursor.execute('select * humanresources.employee')  df = pandas.read_sql(sql, cnxn) cursor.close() cnxn.close() 

but error:

----> 1 df = pandas.read_sql(sql, cnxn)

programmingerror: ('odbc sql type -151 not yet supported. column-index=3 type=-151', 'hy106')

so i'll create answer since know complete context of problem. issue related odbc driver compatibility issues new ms sql server 2016. mentioned able whittle down fields 1 had data type of hierarchyid. based on documentation presented here, can convert nvarchar(4000) string representation. thus, solution in how write query.

where have code as:

cursor.execute('select * humanresources.employee') 

i modify to:

cursor.execute(""" select cast(thehierarchyidfield nvarchar(4000)) myconvertedfield     ,additionalfield     ,... humanresources.employee """) 

i can understand annoying explicitly write fields in query want pull, it's way conversion on sql side before pulling python recognizable data type.

another solution redesign table schema , alter hierarchyid type nvarchar(4000), don't know if have rights alter table i'll propose above solution.

also, if you're planning use pandas pull in data, set sql variable query string , read_sql:

sql = """ select cast(thehierarchyidfield nvarchar(4000)) myconvertedfield     ,additionalfield     ,... humanresources.employee """  df = pandas.read_sql(sql, cnxn) 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -