Does SQLAlchemy support Pervasive PSQL? connection string for it? - python

I googled and found this sqlalchemy-pervasive dialect. I pipped it, but I have no idea how to use it.
What is connection string to pass to create_engine(.....) for sqlalchemy to connect to pervasive psql? is there any other third party driver or dialect?

After I did a:
pip install sqlalchemy-pervasive
This worked for me:
server='localhost'
database='TEST6'
params = urllib.parse.quote_plus(f'DRIVER=Pervasive ODBC Interface;SERVERNAME={server};DBQ={database}')
engine = create_engine("pervasive:///?odbc_connect=%s" % params)

import urllib
import sqlalchemy as sa
params = urllib.parse.quote_plus(f'DRIVER=Pervasive ODBC Interface;SERVERNAME={server}:{port};DBQ={database}')
engine = sa.create_engine("pervasive:///?odbc_connect=%s" % params)
con = engine.connect()
query = 'SELECT * FROM Clientes'
con.execute(query)

Related

Sybase Connection with Flask sqlAlchemy

Im trying to connect Sybase database with flask SQLalchemy using a ODBC connection .
My connection string :
'SQLALCHEMY_DATABASE_URI' = "sybase+pyodbc://username:passw#rd#host:port/dbname?driver=Adaptive+Server+Enterprise"
Getting this Error :
But I'm pretty sure i'm using the right port in the connection string. But when i try connect to connect to the same instance using this method .
Second method :
con = pyodbc.connect(server=server ,port=port ,username=username ,password=password ,driver=driver)
The connection works perfectly fine now with the same connection details.
Can anyone help me in building the connection string URL and help me fix this. Cause I want to use the "db Object" instead of "cursor Object ".
As noted in the Getting Connected wiki page, "Hostname Connections" are not supported. You can either create an ODBC DSN, or use your pyodbc connection string with an ODBC direct pass-through connection:
import urllib
from sqlalchemy import create_engine
connection_string = (
'DRIVER=SAP ASE ODBC driver;'
'SERVER=centos7-vm01;'
'PORT=5000;'
'UID=scott;PWD=tiger;'
'DATABASE=mydatabase;'
'charset=utf8;'
)
connection_uri = f"sybase+pyodbc:///?odbc_connect={urllib.parse.quote_plus(connection_string)}"
engine = create_engine(connection_uri)
After so much research I found that, this error is due incorrect parameter in the connection string.
Invalid port number error will be also fixed with this block of code .
Code here :
import urllib
connection_string = (
'DRIVER=Adaptive Server Enterprise;'
'SERVER=server;'
'PORT=port;'
'UID=username;PWD=password;'
'DATABASE=dbname;'
)
connection_uri = f"sybase+pyodbc:///?odbc_connect={urllib.parse.quote_plus(connection_string)}"
SQLALCHEMY_DATABASE_URI = connection_uri # connection string for SQLALchemy
This code is best solution .

Connect python to Sybase IQ

First of all thank you for your help.
I was trying to retrieve some data from a sybase IQ database using python, but I can not make it.
I've tried with the following code( from https://github.com/sqlanywhere/sqlanydb):
import sqlanydb
conn = sqlanydb.connect(uid='dba', pwd='sql', eng='demo', dbn='demo' )
curs = conn.cursor()
curs.execute("select 'Hello, world!'")
print( "SQL Anywhere says: %s" % curs.fetchone() )
curs.close()
conn.close()
Unfotunately it gives me the following error:
InterfaceError: ('Could not load dbcapi. Tried: None,dbcapi.dll,libdbcapi_r.so,libdbcapi_r.dylib', 0)
Does anyone know how to fix it?
Thanks in advance
Jessica
On windows, first you need to add the data source name (DSN).
You do this by searching for 'odbc data source administrator' on windows and creating a DSN for 'SQL Anywhere 12'. Fill in the necessary information like username,password,host,port,server name and database name. Finally test the connection as shown.
Once finished you can call the code as follows:
import sqlanydb
conn = sqlanydb.connect(dsn='SYBASE_IQ')
curs = conn.cursor()
curs.execute("select 'Hello, world!'")
print( "SQL Anywhere says: %s" % curs.fetchone())
curs.close()
conn.close()
Get and install the SYBASE ODBC DRIVER.
Configure the DSN on your PC.
On Windows, search for the Microsoft ODBC Administrator. Then create a DSN.
Python code:
using SQLAchemy
import sqlalchemy as sa
from sqlalchemy import create_engine, event
from sqlalchemy.engine.url import URL
import urllib
params = urllib.parse.quote_plus('DSN=dsn_name;PWD=user_pwd')
engine = sa.create_engine("sybase+pyodbc:///?odbc_connect={}".format(params))
with engine.connect() as cursor:
cursor.execute(""" SELECT * FROM database """)
Using PyODBC
import pyodbc
conn = pyodbc.connect('DSN=dsn_name;PWD=user_pwd')
with conn:
cursor = conn.cursor()
cursor.execute(""" SELECT * FROM database """)

Python - Convert pyodbc code to SQLAlchemy

I have pyodbc code that I use to connect to a DSN, however for some reason it is no longer working and I cannot figure out why (the drivers are empty even though they are there).
So I want to try and convert everything to use SQLAlchemy instead.
My current code for connecting to the database is:
conn = pyodbc.connect('DSN=QueryBuilder')
cursor = conn.cursor()
stringA = "SELECT GrantInformation.Call FROM GrantInformation"
cursor.execute(stringA)
rows = cursor.fetchall()
How would I get this to do the same in SQLAlchemy, I have checked the documentation and I am still confused.
Many thanks
I used:
from sqlalchemy import create_engine
engine = create_engine("""{}://{}:{}#{}/{}"""
.format(SQL Server,nick,mypassword,myservername,querybuilder))
df = pd.read_sql_query("SELECT GrantInformation.Call FROM GrantInformation")
and I got:
File "<ipython-input-5-f7837462519f>", line 4
.format(SQL Server,nick,mypassword,myservername,querybuilder))
^
SyntaxError: invalid syntax
Also declared the variables before, and I now get:
ArgumentError: Could not parse rfc1738 URL from string 'SQL Server://nick:mypassword#myhost/querybuilder'
from sqlalchemy import create_engine
engine = create_engine("""{}://{}:{}#{}/{}"""
.format(driver,user,password,host,database))
df = pd.read_sql_query("SELECT GrantInformation.Call FROM GrantInformation", engine)
Use one of the below code format to create engine
from sqlalchemy import create_engine
# default
engine = create_engine('mysql://scott:tiger#localhost/foo')
# mysql-python
engine = create_engine('mysql+mysqldb://scott:tiger#localhost/foo')
# MySQL-connector-python
engine = create_engine('mysql+mysqlconnector://scott:tiger#localhost/foo')
# OurSQL
engine = create_engine('mysql+oursql://scott:tiger#localhost/foo')
# query
connection = engine.connect()
result = connection.execute("select username from users")
database name = foo, username = scott, password = tiger, host = localhost
Reference: http://docs.sqlalchemy.org/en/latest/dialects/mysql.html

Connecting to Teradata using Python

I am trying to connect to teradata server and load a dataframe into a table using python. Here is my code -
import sqlalchemy
engine = sqlalchemy.create_engine("teradata://username:passwor#hostname:port/")
f3.to_sql(con=engine, name='sample', if_exists='replace', schema = 'schema_name')
But I am getting the following error -
InterfaceError: (teradata.api.InterfaceError) ('DRIVER_NOT_FOUND', "No driver found for 'Teradata'. Available drivers: SQL Server,SQL Server Native Client 11.0,ODBC Driver 13 for SQL Server")
Can anybody help me to figure out whats wrong in my approach?
There's is different ways to connect to Teradata in Python. The following list is not exhaustive.
SQLAlchemy
If you wish to use SQLAlchemy, you will also need to install the package SQLAlchemy-Teradata. Here is how you can connect:
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base, DeferredReflection
from sqlalchemy.orm import scoped_session, sessionmaker
[...]
# Connect
engine = create_engine('teradata://' + user + ':' + password + '#' + host + ':22/' + database)
db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
db_session.execute('SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;') # To avoid locking tables when doing select on tables
db_session.commit()
Base = declarative_base(cls=DeferredReflection)
Base.query = db_session.query_property()
Then you can use db_session to make queries. See SQLAlchemy Session API
Pyodbc
If you wish to use Pyodbc you will first need to install Teradata driver on your machine. Example on mine, after installing Teradata driver I have the following entry in /etc/odbcinst.ini
[Teradata]
Driver=/opt/teradata/client/16.00/odbc_64/lib/tdata.so
APILevel=CORE
ConnectFunctions=YYY
DriverODBCVer=3.51
SQLLevel=1
Then I can connect with the following:
import pyodbc
[...]
#Teradata Connection
connection= pyodbc.connect("driver={Teradata};dbcname=" + host + ";uid=" + user + ";pwd=" + pwd + ";charset=utf8;", autocommit=True)
connection.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
connection.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
connection.setdecoding(pyodbc.SQL_WMETADATA, encoding='utf-8')
connection.setencoding(encoding='utf-8')
cursor= n.cursor()
cursor.execute("Select 'Hello World'")
for row in cursor:
print (row)
To connect to a teradata database, you need pyodbc, i also have problems with teradata dialect.
Example:
import pyodbc
user = 'user'
pasw = 'pass'
host = 'host'
connection = pyodbc.connect('DRIVER=Teradata;DBCNAME=' + host +';UID=' + user + ';PWD=' + pasw +';QUIETMODE=YES', autocommit=True,unicode_results=True)
I am not sure why your are using sqlalchemy. But you could explore using Teradata module to connect to Teradata as explained in the other link:
Connecting Python with Teradata using Teradata module
I met a similar problem in airflow, I used jars and jaydebeapi to connect teradata database and execute sql:
[root#myhost transfer]# cat test_conn.py
import jaydebeapi
from contextlib import closing
jclassname='com.teradata.jdbc.TeraDriver'
jdbc_driver_loc = '/opt/spark-2.3.1/jars/terajdbc4-16.20.00.06.jar,/opt/spark-2.3.1/jars/tdgssconfig-16.20.00.06.jar'
jdbc_driver_name = 'com.teradata.jdbc.TeraDriver'
host='my_teradata.address'
url='jdbc:teradata://' + host + '/TMODE=TERA'
login="teradata_user_name"
psw="teradata_passwd"
sql = "SELECT COUNT(*) FROM A_TERADATA_TABLE_NAME where month_key='202009'"
conn = jaydebeapi.connect(jclassname=jdbc_driver_name,
url=url,
driver_args=[login, psw],
jars=jdbc_driver_loc.split(","))
with closing(conn) as conn:
with closing(conn.cursor()) as cur:
cur.execute(sql)
print(cur.fetchall())
[root#myhost transfer]# python test_conn.py
[(7734133,)]
[root#myhost transfer]#

Connecting to SQL Server 2012 using sqlalchemy and pyodbc

I'm trying to connect to a SQL Server 2012 database using SQLAlchemy (with pyodbc) on Python 3.3 (Windows 7-64-bit). I am able to connect using straight pyodbc but have been unsuccessful at connecting using SQLAlchemy. I have dsn file setup for the database access.
I successfully connect using straight pyodbc like this:
con = pyodbc.connect('FILEDSN=c:\\users\\me\\mydbserver.dsn')
For sqlalchemy I have tried:
import sqlalchemy as sa
engine = sa.create_engine('mssql+pyodbc://c/users/me/mydbserver.dsn/mydbname')
The create_engine method doesn't actually set up the connection and succeeds, but
iIf I try something that causes sqlalchemy to actually setup the connection (like engine.table_names()), it takes a while but then returns this error:
DBAPIError: (Error) ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect)') None None
I'm not sure where thing are going wrong are how to see what connection string is actually being passed to pyodbc by sqlalchemy. I have successfully using the same sqlalchemy classes with SQLite and MySQL.
The file-based DSN string is being interpreted by SQLAlchemy as server name = c, database name = users.
I prefer connecting without using DSNs, it's one less configuration task to deal with during code migrations.
This syntax works using Windows Authentication:
engine = sa.create_engine('mssql+pyodbc://server/database')
Or with SQL Authentication:
engine = sa.create_engine('mssql+pyodbc://user:password#server/database')
SQLAlchemy has a thorough explanation of the different connection string options here.
In Python 3 you can use function quote_plus from module urllib.parse to create parameters for connection:
import urllib
params = urllib.parse.quote_plus("DRIVER={SQL Server Native Client 11.0};"
"SERVER=dagger;"
"DATABASE=test;"
"UID=user;"
"PWD=password")
engine = sa.create_engine("mssql+pyodbc:///?odbc_connect={}".format(params))
In order to use Windows Authentication, you want to use Trusted_Connection as parameter:
params = urllib.parse.quote_plus("DRIVER={SQL Server Native Client 11.0};"
"SERVER=dagger;"
"DATABASE=test;"
"Trusted_Connection=yes")
In Python 2 you should use function quote_plus from library urllib instead:
params = urllib.quote_plus("DRIVER={SQL Server Native Client 11.0};"
"SERVER=dagger;"
"DATABASE=test;"
"UID=user;"
"PWD=password")
I have an update info about the connection to MSSQL Server without using DSNs and using Windows Authentication. In my example I have next options:
My local server name is "(localdb)\ProjectsV12". Local server name I see from database properties (I am using Windows 10 / Visual Studio 2015).
My db name is "MainTest1"
engine = create_engine('mssql+pyodbc://(localdb)\ProjectsV12/MainTest1?driver=SQL+Server+Native+Client+11.0', echo=True)
It is needed to specify driver in connection.
You may find your client version in:
control panel>Systems and Security>Administrative Tools.>ODBC Data
Sources>System DSN tab>Add
Look on SQL Native client version from the list.
Just want to add some latest information here:
If you are connecting using DSN connections:
engine = create_engine("mssql+pyodbc://USERNAME:PASSWORD#SOME_DSN")
If you are connecting using Hostname connections:
engine = create_engine("mssql+pyodbc://USERNAME:PASSWORD#HOST_IP:PORT/DATABASENAME?driver=SQL+Server+Native+Client+11.0")
For more details, please refer to the "Official Document"
import pyodbc
import sqlalchemy as sa
engine = sa.create_engine('mssql+pyodbc://ServerName/DatabaseName?driver=SQL+Server+Native+Client+11.0',echo = True)
This works with Windows Authentication.
I did different and worked like a charm.
First you import the library:
import pandas as pd
from sqlalchemy import create_engine
import pyodbc
Create a function to create the engine
def mssql_engine(user = os.getenv('user'), password = os.getenv('password')
,host = os.getenv('SERVER_ADDRESS'),db = os.getenv('DATABASE')):
engine = create_engine(f'mssql+pyodbc://{user}:{password}#{host}/{db}?driver=SQL+Server')
return engine
Create a variable with your query
query = 'SELECT * FROM [Orders]'
Execute the Pandas command to create a Dataframe from a MSSQL Table
df = pd.read_sql(query, mssql_engine())

Categories