Transforming pyodbc connection to sqlalchemy syntax - sqlalchemy

I have a connection working with pyodbc:
conn = pyodbc.connect("Driver={};SERVER={};UID={};PWD={}".format(self.DRIVER,self.HOST,self.USER,self.PASS),autocommit=True)
now I want to use sqlalchemy and I trying the bellow code:
c="DRIVER={};SERVER={};UID={};PWD={}".format(self.DRIVER,self.HOST,self.USER,self.PASS)
quoted = quote_plus(c)
new_con = 'mssql+pyodbc:///?odbc_connect={}'.format(quoted)
self.engine = create_engine(new_con)
but i missing something
Can you help me and explain me please?

Use a SQLAlchemy URL object and let it do the quoting for you:
from sqlalchemy.engine import URL
c = (
"Driver=ODBC Driver 17 for SQL Server;"
"Server=192.168.0.199;"
# …
)
conn_url = URL.create(
"mssql+pyodbc",
query={"odbc_connect": c}
)
engine = create_engine(conn_url)

Related

Airflow Connections & pandas.to sql : Don't work together?

In My DAG:
I need use pandas .to_sql
It work with sqlalchemy, but not safety.
It error with connection in Airflow...?
from sqlalchemy import create_engine
# It's works, but not safety!
conn = create_engine('postgresql+psycopg2://login:pass#localhost:5432/de')
# More safe: Make connection in Airflow, PG_PROJECT_CONN
dwh_hook = PostgresHook('PG_PROJECT_CONN')
conn = dwh_hook.get_conn()
# Work OK:
df_ = pd.read_sql(f'select * from stg.restaurants;',conn)
# Don't work: WARNING - /usr/local/lib/python3.8/dist-packages/pandas/io/sql.py:761
# UserWarning: pandas only support SQLAlchemy connectable(engine/connection) ordatabase string URI
# or sqlite3 DBAPI2 connectionother DBAPI2 objects are not tested, please consider using SQLAlchemy
df_restaurants.to_sql('restaurants', conn, if_exists='append', schema='stg', index=False, chunksize=100)

Draw a graph from a SQL request using Dash

I have been searching but I didn't find a simple way to draw a graph from a SQL request.
For example I have this code, and I want to make a bar chart from the result of the request :
import pymysql as sql
from dash import dcc
DB = ...
HOST = ...
USER = ...
PASSWORD = ...
connection = sql.connect(host=HOST,
port=x,
user=USER,
password=PASSWORD,
database=DB,
cursorclass=sql.cursors.DictCursor)
with connection.cursor() as cursor:
# Read a single record
sql = 'SELECT COUNT(*) AS count FROM table'
cursor.execute(sql)
result = cursor.fetchone()
print(result)
Also, I would like to update the chart regularly.
Thank you

Python constructor confusion

I'm playing with python trying to create a basic repository class (normally a C++/C# for work) and am having an issue.
The following code has bombs out on
a = officesRepo(conn) saying "Too many positional arguments for constructor call", but it's being given the only argument specified, the MySql connection object.
I'm coding in vscode on linux using python3.8. I'm wondering if pylint is expecting me to pass in "self", when I don't think it's needed.
Any help/advice/tips greatly received. Flame away if you like, as long as it teaches me something! ;-)
import pymysql.cursors
import Pocos
class officesRepo:
def __init__(conn):
self.conn = conn
def create(office):
pass
def getAll():
cursor = conn.cursor()
SQL = "SELECT `officeCode`, `city`, `phone`, `addressLine1`, `addressLine2`, `state`, `country`, `postalCode`, `territory` "
SQL += "FROM `offices`"
cursor.execute(SQL)
#result = cursor.fetchone()
ret = []
for val in cursor:
ret.append(ret.append(val["officeCode"], val["city"], val["phone"], val["addressLine1"], val["addressLine2"], val["state"], val["country"], val["postalCode"], val["territory"]))
return ret
def getById(id):
pass
conn = pymysql.connect(host='localhost',
user='user',
password='password',
db='classicmodel',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
a = officesRepo(conn)
b = a.getAll()
print(b)
The first parameter of an instance method is self. You don't need to pass it explicitly, but you do need to include it in the parameter list. Right now though, the conn parameter is acting as self, then there's no other parameters after that (thus the error).
You'd need
def __init__(self, conn):
. . .
then similarly for the other methods. All instance methods require an explicit self parameter.

How to download data from mysql connection

After making a connection with mysql library, i'd like to dowload all the database from the connection in my local space (tranforming them into pandas dataframe).
Here's my code:
import MySQLdb
import pandas as pd
conn = MySQLdb.connect(host='host' , user='datbase', passwd='password', db='databases' )
cursor = conn.cursor()
query = cursor.execute(' SELECT * FROM pro ')
df = pd.read_sql(query , conn)
row = cursor.fetchone()
conn.close()
I finnaly got the connection, so i can make some query. But i'd like to use these sql database as a pandas dataframe, '''how can i do it'''?
Just use
query = ' SELECT * FROM pro '
df = pd.read_sql(query , conn)
And df should already be your desired dataframe.

Cannot RESTORE within a transaction; autocommit is on

I'm using sqlalchemy with pyodbc to restore a mssql ".bak" file. I've followed advice from previous posts regarding getting around transactions but it doesn't seem to change anything. Any help would be appreciated.
from urllib.parse import quote_plus
from sqlalchemy import create_engine
params = quote_plus("Driver={SQL Server Native Client 11.0};"
"Server=Computer\SQLEXPRESS;"
"Database=master;"
"Trusted_Connection=yes;")
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
connection = engine.raw_connection()
db_path = r"C:\\Path\\to\\OutputDB.bak"
move_path = r"C:\\Path\\to\\backup\\db.mdf"
move_log_path = r"C:\\Path\\to\\backup\\db_Log.ldf"
sql_cmd = f"""
RESTORE DATABASE [db]
FROM DISK = N'{db_path}'
WITH FILE = 1,
MOVE N'db'
TO N'{move_path}',
MOVE N'test_log'
TO N'{move_log_path}',
RECOVERY,
NOUNLOAD,
REPLACE,
STATS = 5
"""
connection.autocommit = True
cursor = connection.cursor()
cursor.execute(sql_cmd)
while cursor.nextset():
pass
connection.autocommit = False
I get the below error message:
ProgrammingError: ('42000', '[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot perform a backup or restore operation within a transaction. (3021) (SQLExecDirectW); [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]RESTORE DATABASE is terminating abnormally. (3013)')
I managed to fix this by passing connect_args={'autocommit': True} to create_engine. Neither cursor.execute(sql_cmd).execution_options(autocommit=True) or connection.autocommit = True appeared to work.
from urllib.parse import quote_plus
from sqlalchemy import create_engine
params = quote_plus("Driver={SQL Server Native Client 11.0};"
"Server=Computer\SQLEXPRESS;"
"Database=master;"
"Trusted_Connection=yes;")
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params, connect_args={'autocommit': True})
connection = engine.raw_connection()
db_path = r"C:\\Path\\to\\OutputDB.bak"
move_path = r"C:\\Path\\to\\backup\\db.mdf"
move_log_path = r"C:\\Path\\to\\backup\\db_Log.ldf"
sql_cmd = f"""
RESTORE DATABASE [db]
FROM DISK = N'{db_path}'
WITH FILE = 1,
MOVE N'db'
TO N'{move_path}',
MOVE N'test_log'
TO N'{move_log_path}',
RECOVERY,
NOUNLOAD,
REPLACE,
STATS = 5
"""
cursor = connection.cursor()
cursor.execute(sql_cmd)
while cursor.nextset():
pass