I am creating the necessary connections followed by creating a dataframe which I want to send to Azure SQL Database. I am getting stuck at the last part. Any help will be greatly appreciated.
#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)
I got the same error creating \/ replacing a table on Azure synapse until I manually specified dtypes<\/code> as sqlalchemy column types (see pandas documentation<\/a> ):
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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.