How to create sqlalchemy engine from DSN and Azure Authentication - python

I am trying to connect a database using DSN, when I setup the database it did not asked for any username and password, instead i provided the DSN name and selected the security authentication as "Azure Active Directory Integrated Authentication".
I can connect like this
conn = pyodbc.connect(DSN='EAP') # EAP is my DSN name, and it works fine
df = pd.read_sql(myquery, conn)
conn.close()
# this query does not fail, just gives the warnings.
# I would like to use engine method, instead of this connection method.
warning:
pandas only support SQLAlchemy connectable(engine/connection)
ordatabase string URI or sqlite3 DBAPI2 connectionother DBAPI2
objects are not tested, please consider using SQLAlchemy
Question: How to get the same query using engine, instead of conn?
My Attempts
import sqlalchemy
from sqlalchemy.engine import URL
from sqlalchemy import create_engine
#conn_str = "DRIVER={ODBC Driver 17 for SQL Server};DSN=EAP"
conn_str = "DRIVER={msodbcsql17.dll};DSN='EAP'"
conn_url = URL.create("mssql+pyodbc", query={"odbc_connect": conn_str})
engine = create_engine(conn_url)
# engine = sqlalchemy.create_engine('mysql+pyodbc://EAP')
parent = pd.read_sql(myquery, engine)
Gives ERROR:
InterfaceError: (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)
References:
There are literally infinite materials similar to this question, but none of them could solve my case.
https://docs.sqlalchemy.org/en/14/dialects/mssql.html#module-sqlalchemy.dialects.mssql.pyodbc
Pandas to_sql not working with SQL Alchemy connection
sqlalchemy, specify database name with DSN

Related

SQLAlchemy with Azure Synapse (SQL) - SAWarning: Could not fetch transaction isolation level

Trying to connect a SQL Synapse database with read-only access, Followed the advice re autocommit here https://docs.sqlalchemy.org/en/14/dialects/mssql.html
However receive the following error...
SAWarning: Could not fetch transaction isolation level, tried views:
('sys.dm_exec_sessions', 'sys.dm_pdw_nodes_exec_sessions'); final
error was: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL
Server][SQL Server]User does not have permission to perform this
action. (6004) (SQLExecDirectW)')
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
conn_string = "DRIVER={ODBC Driver 17 for SQL Server};" \
"SERVER=mydatabase.database.windows.net;" \
"DATABASE=somedbname;" \
"AUTHENTICATION=ActiveDirectoryInteractive;" \
"UID=gary#test.com;"
connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": conn_string, "autocommit": "true"})
print(connection_url)
engine = create_engine(connection_url).execution_options(
isolation_level="AUTOCOMMIT"
)
with engine.connect() as con:
con.execute("SELECT 1")
Must the read-only uses still require access to these views?

Python SQL Alchemy connection to MSSQL Error

I'm using below pypyodbc library in python to connect with our SQL Server Database and it works perfectly.
import pypyodbc as odbc
conn = odbc.connect('DRIVER={SQL
Server};SERVER=serverv1;DATABASE=CMS;UID=Test;PWD=Test')
Now I want to use SQL Alchemy library to upload my excel data to our SQL Server database, however when I apply the same connection properties, I got an error.
from sqlalchemy import create_engine
import pandas as pd
import os
#dialect+driver://username:password#host:port/database --> Syntax
engine = create_engine('mssql+pyodbc://Test:Test#serverv1/CMS')
cwd = os.getcwd()
XLFile = (cwd +'/FILES/MARCH.xlsx')
xl = pd.read_excel(XLFile,sheet_name='SALES')
xl.to_sql('WES', engine, if_exists='append', index=False)
print(xl)
I used the same log in info but unsuccessful in SQL Alchemy. I already tried some of the samples here but no luck. Please help need to establish my connection.
Error encountered.
File
"C:\Users\test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sqlalchemy\engine\default.py", line 597, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.InterfaceError: (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)
It works using pymssql, instead of pyodbc.
Install pymssql using pip, then change your code to:
engine = sqlalchemy.create_engine("mssql+pymssql://username:password#servername/dbname")
if it doesn't work downgrade your sqlalchemy

How to connect to the sql azure database with python SQL alchemy using active directory integrated authentication

I am using the connection string as below
params=parse.quote_plus("Driver={ODBC Driver 17 For SQL server};Server=tcp:server name,1433;database=database name;Encrypt=yes;TrustServerCertificate=no;Authentication=ActiveDirectoryIntegrated'
engine=sqlalchemy.create_engine("mssql:///?odbc_connect=%s" %params)
using the above connection string it is giving me the error
[Microsoft][ODBC Driver 17 for SQL server][SQL server]111214 an attempt to an attempt to complete the transaction has failed no corresponding transaction found
UPDATE
You can add connect_args, then try.
Please make sure you have same account login your windows pc and sql server.
engine = create_engine('mssql+pyodbc:///?odbc_connect=%s' % params, echo=True, connect_args={'autocommit': True})
PREVIOUS
You can consider to use Authentication=ActiveDirectoryPassword which be easier than Authentication=ActiveDirectoryIntegrated, and the code as below which is works for me.
Thank for Peter Pan's answer, for more details, you can refer his description. His answer has detailed usage of Authentication=ActiveDirectoryIntegrated in his description, I prefer Authentication=ActiveDirectoryPassword, so I posted my answer, you can refer to it.
How to connect to Azure sql database with python SQL alchemy using Active directory integrated authentication
from urllib import parse
from sqlalchemy import create_engine
your_user_name = 'pa**i#**a.onmicrosoft.com'
your_password_here = 'J***20'
connecting_string = 'Driver={ODBC Driver 17 for SQL Server};Server=tcp:yoursqlserver.database.windows.net,1433;Database=yoursqldb;Uid='+your_user_name+';Pwd='+your_password_here+';Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryPassword'
params = parse.quote_plus(connecting_string)
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
connection = engine.connect()
result = connection.execute("select 1+1 as res")
for row in result:
print("res:", row['res'])
connection.close()

Connect Python to Teradata Connection Error

I'm using sqlalchemy trying to connect to Teradata via ODBC as I need to be able to read/write to Teradata.
from sqlalchemy import create_engine
import sqlalchemy_teradata
user = 'user'
pasw='pasw'
host = 'host'
# connect
td_engine = create_engine("teradata://"+user+":"+pasw+"#"+host+"/?authentication=ODBC?driver=Teradata")
#execute sql
sql = "select * from table"
result = td_engine.execute(sql)
However I get the following error.
(teradata.api.DatabaseError) (0, '[28000] [Teradata][ODBC Teradata Driver] User Specified Mechanism for Logon is Not Available')
(Background on this error at: http://sqlalche.me/e/4xp6)
The link provided is not very informative unless I'm missing something. The error is from Teradata but I'm not sure what it actually means. It looks like it saying that I can't use ODBC? Any suggestions or alternatives?

pyodbc InterfaceError: sqlalchemy.create_engine(mssql+pyodbc://...) [duplicate]

I'm using pyodbc to connect SQL Server. I had created connection string like this:
from sqlalchemy import Table, Column, databases, Integer, String, ForeignKey, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import session
engine = create_engine('mssql+pyodbc://sa:123#localhost/TrainQuizDB')
engine.connect()
TrainQuizDB is database name that I created in Sql Server.
For more information I have windows 8.1 64bit and I installed python version 3.5.1 32bit and I downloaded pyodbc from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyodbc (pyodbc-3.0.10-cp35-none-win32.whl).
But when I try to connect it cause this error:
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Also I have tested the connection In ODBC Data sources and it was successful.
As noted in the relevant section of the SQLAlchemy documentation:
Hostname-based connections are not preferred, however are supported. The ODBC driver name must be explicitly specified
So, you need to add the driver name to your connection string:
engine = create_engine('mssql+pyodbc://sa:123#localhost/TrainQuizDB?driver=ODBC+Driver+17+for+SQL+Server')

Categories