Connect Python to Teradata Connection Error - python

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?

Related

How to create sqlalchemy engine from DSN and Azure Authentication

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

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

Oracle Connection in Python Using create_engine

I am trying to connect to an oracle database in Python using create_engine. This database does not have a username or password.
I see this is the format now:
oracle_db = sqlalchemy.create_engine('oracle://user:pass#server').
However, if this connection has NO username or password, how would the connection string look? I've tried DMIT_connection = create_engine('oracle+cx_oracle://#....) with no luck. When I go to write a pandas df to the database using to_sql I get the error below because I cannot get the connection right give that there is no username or password.
The error occurs because this database has no username (picked up from the localhost machine) and there is no password.
The error I get is this: DatabaseError: (cx_Oracle.DatabaseError) ORA-12545: Connect failed because target host or object does not exist (Background on this error at: http://sqlalche.me/e/14/4xp6)
Let me know authentication type used. If its external authentication , picking credentials from wallet, you can try sample code mentioned here
How to use SqlAlchemy to connect Database similar to cx_oracle when we use external authorization like wallets with TNS(net service name)

Flask-SQLAlchemy: "Login failed for user" connecting to MSSQL Database. macOS

Newbie to Flask and Flask-SQLAlchemy. Attempting to connect to a remote MSSQL database.
Toolsets:
macOS 10.13 (High Sierra)
Python3
Flask
Flask-SQLAlchemy
FreeTDS
pyodbc
If I set up a connection using a standard flask-sqlalchemy URI like this:
app.config['SQLALCHEMY_DATABASE_URI'] = 'mssql+pyodbc://<username>:<password>#<servername>/<database name>?port=1433&driver=FreeTDS&tds_version=7.3'
I end up with this error message:
sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [FreeTDS][SQL Server]Login failed for user '<username>'. (18456) (SQLDriverConnect)")
Interestingly, if I just use a straight pyodbc connection outside of flask it works. That code:
cnxn = pyodbc.connect('DRIVER={FreeTDS};TDS_Version=7.3;SERVER=<servername>;PORT=1433;DATABASE=<database name>;UID=<username>;PWD=<password>')
I either need to solve the connection issue via Flask-SQLAlchemy or to know if it's possible to create the connection with the straight pyodbc.connect statement and somehow pass that into Flask-SQLAlchemy.
Thanks for any guidance or ideas.
Update
I have also tried:
params = urllib.parse.quote_plus('DRIVER={FreeTDS};SERVER=<servername>;DATABASE=<database name>;PORT=1433;UID=<username>;PWD=<password>;TDS_Version=7.3')
app.config['SQLALCHEMY_DATABASE_URI'] = "mssql+pyodbc:///?odbc_connect=%s" % params
But I get the same 'login failed' error.

Python sqlalchemy trying to write pandas dataframe to SQL Server using .to_sql

I have a python code through which I am getting a pandas dataframe "df". I am trying to write this dataframe to Microsoft SQL server. I am trying to connect through the following code by I am getting an error
import pyodbc
from sqlalchemy import create_engine
engine = create_engine('mssql+pyodbc:///?odbc_connect=DRIVER={SQL Server};SERVER=bidept;DATABASE=BIDB;UID=sdcc\neils;PWD=neil!pass')
engine.connect()
df.to_sql(name='[BIDB].[dbo].[Test]',con=engine, if_exists='append')
However at the engine.connect() line I am getting the following error
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]Neither DSN nor SERVER keyword supplied (0) (SQLDriverConnect)')
Can anyone tell me what I am missing. I am using Microsoft SQL Server Management Studio - 14.0.17177.0
I connect to the SQL server through the following
Server type: Database Engine
Server name: bidept
Authentication: Windows Authentication
for which I log into my windows using username : sdcc\neils
and password : neil!pass
I am new to databases and python. Kindly let me know if you need any additional details. Any help will be greatly appreciated. Thank you in advance.
I was finally able to make it run.
import pyodbc
from sqlalchemy import create_engine
import urllib
params = urllib.quote_plus(r'DRIVER={SQL Server};SERVER=bidept;DATABASE=BIDB;Trusted_Connection=yes')
### For python 3.5: urllib.parse.quote_plus
conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)
engine = create_engine(conn_str)
reload(sys)
sys.setdefaultencoding('utf8')
df.to_sql(name='Test',con=engine, if_exists='append',index=False)
Thanks to #gord-thompson who answered Here
Although my in my sql server, all the tables are under the 'dbo' schema (i.e. dbo.Test1, dbo.Other_Tables) and this query puts my table in 'sdcc\neils' schema (i.e. sdcc\neils.Test1, sdcc\neils.Other_Tables) any solution to this?

Categories