I am trying to establish a connection to SQL Server so I can upload my df to a table (using .to_sql()
) and run a query later on with this new table. My issue is that when trying to establish a connection to SQL Server (work database is on SQL Server ) I get the same error message when trying a couple of different methods.
#Method 1
TR_info = pd.read_excel(TR_path, sheet_name = 'TR information', skiprows = 3)
cnxn_str = (r"Driver={SQL Server Native Client 11.0};"
r"Server=server_name;"
r"Database=ALDB;"
r"Trusted_Connection=yes;")
cnxn = pyodbc.connect(cnxn_str)
TR_info.to_sql(name='tr_info', schema='RF1', con = cnxn, index=False, if_exists='replace')
#Method 2
TR_info = pd.read_excel(TR_path, sheet_name = 'TR information', skiprows = 3)
import sqlalchemy as sal
engine = sal.create_engine('mssql+pyodbc://server_name/ALDB?driver=SQL Server Native Client 11.0?Trusted_Connection=yes')
conn = engine.connect()
TR_info.to_sql(name='tr_info', schema='RF1', con = conn, index=False, if_exists='replace')
Both methods give me the following error message.
(pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') (Background on this error at: https://sqlalche.me/e/14/rvf5)
Finally, I have also confirmed I have SQL Native client 11.0 setup in my ODBC Data Source Administrator, so it is not that.
I am using a different driver, but maybe try the following syntax using quote_plus :
from urllib.parse import quote_plus
SQL_SERVER_NAME = ''
SQL_DB_NAME = ''
SQL_USER = ''
SQL_PW = ''
odbc_connection_string = f"Driver={{ODBC Driver 17 for SQL Server}};Server=tcp:{SQL_SERVER_NAME}.database.windows.net,1433;Database={SQL_DB_NAME};Uid={SQL_USER}@{SQL_SERVER_NAME};Pwd={SQL_PW};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"
SQLALCHEMY_DATABASE_URI = f"mssql+pyodbc:///?odbc_connect={quote_plus(odbc_connection_string)}"
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.