【问题标题】:pyodbc.ProgrammingError '42000', [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot find data type 'TEXT'. (104220) (SQLExecDirectW)pyodbc.ProgrammingError '42000',[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]找不到数据类型'TEXT'。 (104220) (SQLExecDirectW)
【发布时间】:2021-09-01 12:16:59
【问题描述】:

我正在创建必要的连接,然后创建要发送到 Azure SQL 数据库的数据框。 我在最后一部分卡住了。 任何帮助将不胜感激。

#The last line of code gives me the programming error as stated in the question
#Please, please try to help me with this , I will be eternally grateful

#Creating connections

import pandas as pd
from sqlalchemy import create_engine, MetaData, Table, select
from six.moves import urllib

params = urllib.parse.quote_plus(r'Driver={ODBC Driver 17 for SQL 
Server};Server=tcp:abcd.sql.azuresynapse.net,1433;Database=xxx;Uid=yyy;Pwd= 
{zzz};Encrypt=yes;TrustServerCertificate=yes;Connection Timeout=30;')
conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)
engine = create_engine(conn_str,connect_args={'autocommit': True})
engine.connect() 
 

#Create dataframe

df=pd.DataFrame(columns=['Name','Subject','Marks','GPA'])
df['Name']=['A','B','C','D','E']
df['Subject']=['IUDI','KDBJSCJ','SJIJSABCIBSA','DCOSANNOA','SDOISD']
df['Marks']=[659 for i in range(0,5)]
df['GPA']=[8.0 for i in range(0,5)]
 
#Export Dataframe to sql (Problem code)
df.to_sql(name='demo_table',con=engine,index=False)

【问题讨论】:

  • 嗨@bkghosh,如果我在答案中理解错误,请纠正我。 :)

标签: python azure azure-synapse


【解决方案1】:

在我手动将 dtypes 指定为 sqlalchemy 列类型之前,我在 Azure 突触上创建/替换表时遇到了同样的错误(请参阅 pandas documentation):

from sqlalchemy.dialects.mssql import NVARCHAR, INTEGER, FLOAT
col_types = {"Name": NVARCHAR(1),
             "Subject": NVARCHAR(12),
             "Marks": INTEGER,
             "GPA": FLOAT}
df.to_sql(name="demo_table", con=engine, dtype=col_types, if_exists="replace")

这并不是 Azure Synapse 第一次得到比普通 SQL Server 更差的支持。我想它只是在推断数据类型方面存在问题。

【讨论】:

    猜你喜欢
    • 2021-06-03
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    相关资源
    最近更新 更多