am trying to connect with MSSQL remotely which is in windows from ubuntu using sqlalchemy.I creted DSN like below
dbinfo.py:
username = 'XXX'
pw = 'XXX'
host = '190.122.12.214'
drivername = 'SQL Server'
database = 'XXX'
extra_param=''
and i mported the dbinfo.py file into db_handler.py :
import transaction
from z3c.saconfig import Session as SASession
from z3c.saconfig import EngineFactory
from zope import component
from zope.sqlalchemy import mark_changed
# sqlalchemy
import sqlalchemy as sa
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from redindia.loginpage import dbinfo
info = {
'username' : dbinfo.username,
'pw' : dbinfo.pw,
'host' : dbinfo.host,
'drivername' : dbinfo.drivername,
'database' : dbinfo.database,
'extra_param' : ''
}
drivername = str(info['drivername'])
username = str(info['username'])
host = str(info['host'])
database = str(info['database'])
extra_param = str(info['extra_param'])
def getDb():
pass
def getSession(testing=False):
try:
return SASession()
except component.ComponentLookupError:
pass
# construct url to open database
_testing_ = ''
if testing:
_testing_ = '_testing'
if info['pw'] != '':
DSN = drivername+'://'+username+':' + info['pw'] +'#'+host+'/'+database+_testing_+'?charset=utf8'+extra_param
else:
DSN = drivername+'://'+username+'#'+host+'/'+database+_testing_+'?charset=utf8'+extra_param
engine_factory = EngineFactory(DSN, pool_recycle=7200)
engine = engine_factory()
## create a global session
from z3c.saconfig import GloballyScopedSession
utility = GloballyScopedSession(bind=engine) # i think, without engine, it will find above provided one...
from z3c.saconfig.interfaces import IScopedSession
component.provideUtility(utility, provides=IScopedSession)
return SASession()
session = getSession()
engine = session.get_bind()
Base = declarative_base(engine)
Base.metadata.reflect()
tables = Base.metadata.tables
and then connecting details below mentioned
def callStoreProcedure(self):
form = self.request.form
area = form.get('Area')
session = getSession()
result = session.execute("select * from BBBB")
result_set = result.fetchall()
return result_set
and i configure ODBC connectivity settings
etc/odbc.ini:
[SQL Server]
Description=my dsn
Driver=SQL Server
Database=XXX
Servername=190.122.12.214
UID=XXX
PWD=XXX
etc/odbcinst.ini:
[SQL Server]
Description = sql Driver
Driver = /usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsS.so
UsageCount = 1
I configured the settings like above.But i can't able to connect MSSQL.am getting the error like below
"ArgumentError: Could not parse rfc1738 URL from string 'SQL Server://XXX:XXX#190.122.12.214/XXX?charset=utf8'"
Plz can anyone help me to solve this issues.Thanks in advance.
The SQLAlchemy engine URL should begin with either mssql or mssql+pyodbc. See the Engine Configuration documentation.
I created dbhandler.py file. It contains the details about the database connectivity.The details are below
db_handler.py:
from sqlalchemy import create_engine
def getSession(self):
DSN="mssql://UID:PWD#IPADDRESS/DBNAME"
return DSN
our .py file
from xxxx.yyyy.db_handler import getSession
from sqlalchemy import create_engine
def callStoreProcedure(self):
form = self.request.form
DSN = getSession(self)
engine = create_engine(DSN)
cursor = engine.execute("select * from tablename")
result = cursor.fetchall()
return result
Now i have connected with the database.
Related
When I Try to connect oracle server with SQLAlchemy. I'm getting this error.
NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:oracle.oracledb
from sqlalchemy.engine import create_engine
DIALECT = 'oracle'
SQL_DRIVER = 'oracledb'
USERNAME = 'username' #enter your username
PASSWORD = 'password' #enter your password
HOST = 'host url' #enter the oracle db host url
PORT = 1533 # enter the oracle port number
SERVICE = 'service name' # enter the oracle db service name
ENGINE_PATH_WIN_AUTH = DIALECT + '+' + SQL_DRIVER + '://' + USERNAME + ':' + PASSWORD +'#' + HOST + ':' + str(PORT) + '/?service_name=' + SERVICE
engine = create_engine(ENGINE_PATH_WIN_AUTH)
#test query
import pandas as pd
test_df = pd.read_sql_query('SELECT * FROM global_name', engine)
any different method to connect?
For completeness (since the answer is already in comments): with SQLAlchemy 1.4 add this to your top level script file:
import sys
import oracledb
oracledb.version = "8.3.0"
sys.modules["cx_Oracle"] = oracledb
and then proceed as if you were using cx_Oracle. The create_engine() should begin with oracle: like:
# SQLAlchemy 1.4 with python-oracledb or cx_Oracle
engine = create_engine('oracle://...
The sys.modules etc snippet isn't needed for SQLAlchemy 2.0. With this version create_engine() should begin with oracle+oracledb: like:
# SQLAlchemy 2.0 with python-oracledb
engine = create_engine('oracle+oracledb://...
These posts are good references:
Using python-oracledb 1.0 with SQLAlchemy, Pandas, Django and Flask
Using SQLAlchemy 2.0 (development) with python-oracledb for Oracle Database
Failed to get the response. Hanging? method: post, url: https://ABC.us-east-2.aws.snowflakecomputing.com:443/session/v1/login-request?request_id=efabf1c7-7fb6-48cb-a50e-256967e3de45&databaseName=DATAVAULT&schemaName=STAGE&warehouse=DATAVAULT&roleName=TEST_Admin&request_guid=c99547cd-ceeb-4280-845b-f2de7be76755
Getting connection error on Windows Python 3.8
Connection parameters
{
"user" : "TestUser1",
"password" : "XXXX",
"account" : "ABC.us-east-2.aws",
"warehouse" : "DATAVAULT",
"database" : "DATAVAULT",
"schema" : "STAGE",
"Role" : "Test_role"
}
Also tried account name as "ABC", "ABC.aws" but didn't work
Code:
import snowflake.connector as snow
conn = snow.connect(user=self.user,
password= self.password,
account= self.account,
warehouse=self.warehouse,
database=self.database,
schema=self.schema,
role=self.role
)
cur = conn.cursor()
The same code works fine from Linux but not with Windows version of Python.
Added proxy as below but didn't help
os.environ['HTTP_PROXY'] = "http://http-proxy.company.com:80"
os.environ['HTTPS_PROXY'] = "https://http-proxy.company.com:443"
=====================================
Using sqlalchemy
import os
os.environ['HTTP_PROXY'] = "http://http-proxy.xxx.com:80"
os.environ['HTTPS_PROXY'] = "https://https-proxy.xxx.com:443"
from sqlalchemy import create_engine
from snowflake.sqlalchemy import URL
import os
import sys
url = URL(
account = 'xxxx.us-east-2.aws',
user = 'TestUser1',
database = 'DATAVAULT' ,
schema = 'STAGE',
warehouse= 'DATAVAULT',
role = 'DV_Admin',
password='xxxx', # note my passsword has a '#' in it
)
print(os.environ['HTTP_PROXY'])
print(os.environ['HTTPS_PROXY'])
engine = create_engine(url)
print(engine)
results = engine.execute('select current_version()').fetchone()
sys.exit()
try:
results = engine.execute('select current_version()').fetchone()
assert results is not None
finally:
engine.dispose()
After fixing the Wifi proxy and Key chain file I am able to connect from my MAC.
I have this code
from flask_mysqldb import MySQL
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER']='root'
app.config['MYSQL_PASSWORD']=''
app.config['MYSQL_DB']='appdb'
mysql = MySQL(appdb)
cursor = mysql.connection.cursor()
cursor.execute("SELECT id FROM table WHERE id='Apple'")
apple= cursor.fetchone()
cursor.close()
However, I get this error
Attribute Error: Nonetype object has no attribute 'cursor'
How to solve this error?
from sqlalchemy import create_engine
from sqlalchemy.orm import close_all_sessions
import mysql.connector
import time
username = 'MYSQL_USER'
password = 'MYSQL_PASSWORD!'
host = 'MYSQL_HOST'
port = 'MYSQL_port'
DB_NAME = 'MYSQL_DBname'
engine = create_engine(f"mysql+mysqlconnector://{username}:{password}#{host}:{port}")
n = 250
for i in range(0,vac.shape[0],n):
with engine.connect() as conn:
result = conn.execute("USE MYSQL_DBname")
result = conn.execute(""SELECT id FROM table WHERE id='Apple'"")
try this one
Your parameters are in "app" not "appdb" which is why "mysql" is None, in other words no connection was made. Change it to MySQL(app).
It's better to check if the connection was made before executing a query.
from flask import Flask
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER']='root'
app.config['MYSQL_PASSWORD']=''
app.config['MYSQL_DB']='appdb'
mysql = MySQL(app)
if mysql:
print("Connection Successful!")
cursor = mysql.connection.cursor()
cursor.execute("SELECT id FROM table WHERE id='Apple'")
apple= cursor.fetchone()
cursor.close()
else:
print("Connection Failed!")
if __name__ == '__main__':
app.run()
I need to read from AWS- Aurora table and write the content to Oracle table.
My code is-
import pandas as pd
import psycopg2
from sqlalchemy import types, create_engine
import cx_Oracle
import sys
**# Connect to Aurora**
host = sys.argv[1]
username = sys.argv[2]
password = sys.argv[3]
database = sys.argv[4]
db_conn = psycopg2.connect(host=host, database=database, user=username, password=password)
sql = "SELECT * FROM Table_Name;"
data_df = pd.io.sql.read_sql(sql, db_conn)
print(data_df.head(2))
db_conn.close()
# Connect to Oracle and write data_df dataframe
dsn = cx_Oracle.makedsn('10.z.y.xx', '1521', service_name='abcd')
u_name = sys.argv[5]
pwd = sys.argv[6]
conn = cx_Oracle.connect(user=u_name, password=pwd, dsn=dsn)
ora_engine = create_engine(f'oracle+cx_oracle://{u_name}:{pwd}#{dsn}', echo=True)
ora_engine.connect()
data_df.to_sql(name='oracle_table_name', con=conn)
conn.close()
Connect to Aurora is working but I'm unable to create engine in Oracle and write the dataframe!
The code is correct, due to high volume of data and low RAM being configured, it was failing.
Thanks.
I am trying to connect to an Azure database using SQLAlchemy in Python.
My code is the following:
engine_azure = \
create_engine('mssql+pyodbc://{Server admin login}:{password}#{Server name}.database.windows.net:1433/{AdventureWorksLT}', echo=True)
I get the following message:
C:\ProgramData\Anaconda3\lib\site-packages\sqlalchemy\connectors\pyodbc.py:92: SAWarning: No driver name specified; this is expected by PyODBC when using DSN-less connections
"No driver name specified; "
Then I run the following code:
print(engine_azure.table_names())
I get the following message:
DBAPIError: (pyodbc.Error) ('01S00', '[01S00] [Microsoft][ODBC Driver Manager] Invalid connection string attribute (0) (SQLDriverConnect)')
There are 2 issues with your connection string:
As per the SQLAlchemy documentation: The delimeters must be URL escaped when using a pass-through exact pyodbc string.
And you do not specify the sql driver name either.
You can use the code below, which works fine at my side:
import pyodbc
from sqlalchemy import create_engine
import urllib
params = urllib.parse.quote_plus \ # urllib.parse.quote_plus for python 3
(r'Driver={ODBC Driver 13 for SQL Server};Server=tcp:yourDBServerName.database.windows.net,1433;Database=dbname;Uid=username#dbserverName;Pwd=xxx;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;')
conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)
engine_azure = create_engine(conn_str,echo=True)
print('connection is ok')
print(engine_azure.table_names())
Test result:
And for the connection string, you can get it by going to azure portal -> your database -> connection strings(select the ODBC in this case):
This is what i use in Python3:
params = urllib.parse.quote_plus(
'Driver=%s;' % driver +
'Server=tcp:%s,1433;' % server +
'Database=%s;' % database +
'Uid=%s;' % username +
'Pwd={%s};' % password +
'Encrypt=yes;' +
'TrustServerCertificate=no;' +
'Connection Timeout=30;')
conn_str = 'mssql+pyodbc:///?odbc_connect=' + params
engine = create_engine(conn_str)
Python3 snippet that I am using with ODBC Driver 17 for SQL Server. Cost me some time to figure it all out, especially the driver version and params.
import urllib
from sqlalchemy import create_engine
driver = "{ODBC Driver 17 for SQL Server}"
server = "<server-name>.database.windows.net"
database = "<db-name>"
user = "<db-user>"
password = "<db-password>"
conn = f"""Driver={driver};Server=tcp:{server},1433;Database={database};
Uid={user};Pwd={password};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"""
params = urllib.parse.quote_plus(conn)
conn_str = 'mssql+pyodbc:///?autocommit=true&odbc_connect={}'.format(params)
engine = create_engine(conn_str, echo=True)
engine.execute("SELECT 1")
Furthermore, I needed to install the following drivers/tools on macOS:
brew install msodbcsql17 mssql-tools
Step 1: Install Azure SQL DB Drivers
Install the new version of SQL DB Drivers using official documentation:
Linux, MacOS, Windows
Major update to previous answers: use the last supported version of DB driver ODBC Driver 17 for SQL Server instead of outdated versions ODBC Driver 13 for SQL Server or versions without explicitly defined a version, e.g. SQL Server.
Step 2: Install sqlalchemy package
Just print in the terminal: pip install SQLAlchemy
Step 3: Wrapping specific DB logic in AzureDbConnection class
from dataclasses import dataclass
from typing import Dict, Any, Iterable
from pandas import DataFrame
from sqlalchemy import create_engine, inspect
import urllib
#dataclass(frozen=True)
class ConnectionSettings:
"""Connection Settings."""
server: str
database: str
username: str
password: str
driver: str = '{ODBC Driver 18 for SQL Server}'
timeout: int = 30
class AzureDbConnection:
"""
Azure SQL database connection.
"""
def __init__(self, conn_settings: ConnectionSettings, echo: bool = False) -> None:
conn_params = urllib.parse.quote_plus(
'Driver=%s;' % conn_settings.driver +
'Server=tcp:%s.database.windows.net,1433;' % conn_settings.server +
'Database=%s;' % conn_settings.database +
'Uid=%s;' % conn_settings.username +
'Pwd=%s;' % conn_settings.password +
'Encrypt=yes;' +
'TrustServerCertificate=no;' +
'Connection Timeout=%s;' % conn_settings.timeout
)
conn_string = f'mssql+pyodbc:///?odbc_connect={conn_params}'
self.db = create_engine(conn_string, echo=echo)
def connect(self) -> None:
"""Estimate connection."""
self.conn = self.db.connect()
def get_tables(self) -> Iterable[str]:
"""Get list of tables."""
inspector = inspect(self.db)
return [t for t in inspector.get_table_names()]
def dispose(self) -> None:
"""Dispose opened connections."""
self.conn.close()
self.db.dispose()
Major update to previous answers: do not forget to close the connection and dispose of the DB engine explicitly as soon as it stops being needed.
Enjoy!
Set connection settings and credentials using Azure DB blade on Azure Portal:
conn_settings = ConnectionSettings(
server='<db_server_name>',
database='<db_name>',
username='<user_name>',
password='***')
Open DB connection:
db_conn = AzureDbConnection(conn_settings)
db_conn.connect()
Test connection (for example, get available tables list), do other stuff, and close it finally:
try:
for t in db_conn.get_tables():
print(t)
# Do another DB-related stuff:
# ...
finally:
db_conn.dispose()
None of the solutions posted so far worked for me.
Instead, I had to specify Driver ({SQL Server}) which worked perfectly.
params = urllib.parse.quote_plus("DRIVER={SQL Server};SERVER=sqlhost.database.windows.net;DATABASE=pythonSQL;UID=username#sqldb;PWD=password56789")
conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)
engine_azure = create_engine(conn_str,echo=True)
Source:
https://gist.github.com/timmyreilly/f4a351eda5dd45aa9d56411d27573d7c