Python and SQLAlchemy



A quick example of how to create a SQLAlchemy engine connection string in python.

from sqlalchemy import *
import pyodbc
engine = create_engine("mssql+pyodbc://SERVER\\INSTANCE/DATABASE?driver=SQL+Server+Native+Client+11.0")



Once defined we can use the engine to feed dataframes into our SQL instances such as :



df.to_sql("TABLE", engine, if_exists='append',index = False, chunksize = 200)



Additionally we can run selects from the engine we defined and read that data straigth into a data frame.

df = pd.read_sql_query("SELECT top 10 * from XXXX", engine)

Here is an example of using SqlAlchemy to retieve stock prices and stock in a SQL server database. Example

Ian Fogelman

Ian Fogelman

My name is Ian Fogelman. I like to develop data driven solutions with SQL Server, Python, .NET and predictive analytics.