2017-04-03T20:16:40
Connecting to Microsoft SQL Server with Python and SQLAlchemy
from sqlalchemy.sql import text
from sqlalchemy import create_engine
import secrets
engine = create_engine(
'mssql+pyodbc://{}:{}@MSSQL'.format(
secrets.username, secrets.password
)
)
conn = engine.connect()
s = text("SELECT * FROM users WHERE name = :name")
result = conn.execute(s, name=name).fetchall()
print result
For further information on installing and configuring unixODBC (requirement for pyodbc) on Linux:
https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-RHEL-or-Centos
Connecting to Microsoft SQL Server with pyodbc:
http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#dialect-mssql-pyodbc-connect