Skip to content

Instantly share code, notes, and snippets.

View phillipsj's full-sized avatar

Jamie Phillips phillipsj

View GitHub Profile
$ python
>>> from sqlalchemy import create_engine
>>> engine = create_engine("mssql+pyodbc://sqladmin:[email protected]:1433/adventureworks?driver=ODBC+Driver+17+for+SQL+Server")
>>> con = engine.connect()
>>> rows = con.execut('SELECT TOP 10 * FROM SalesLT.ProductCategory')
>>> [print(row) for row in rows]
(1, None, 'Bikes', 'CFBDA25C-DF71-47A7-B81B-64EE161AA37C', datetime.datetime(2002, 6, 1, 0, 0))
(2, None, 'Components', 'C657828D-D808-4ABA-91A3-AF2CE02300E9', datetime.datetime(2002, 6, 1, 0, 0))
(3, None, 'Clothing', '10A7C342-CA82-48D4-8A38-46A2EB089B74', datetime.datetime(2002, 6, 1, 0, 0))
(4, None, 'Accessories', '2BE3BE36-D9A2-4EEE-B593-ED895D97C2A6', datetime.datetime(2002, 6, 1, 0, 0))
from sqlalchemy import create_engine
def query_categories():
engine = create_engine(
"mssql+pyodbc://sqladmin:[email protected]:1433/adventureworks?driver=ODBC+Driver+17+for+SQL+Server")
# The with statement cleans up the connection resource for us.
with engine.connect() as con:
rows = con.execute('SELECT TOP 10 * FROM SalesLT.ProductCategory')
$ python query.py
(1, None, 'Bikes', 'CFBDA25C-DF71-47A7-B81B-64EE161AA37C', datetime.datetime(2002, 6, 1, 0, 0))
(2, None, 'Components', 'C657828D-D808-4ABA-91A3-AF2CE02300E9', datetime.datetime(2002, 6, 1, 0, 0))
(3, None, 'Clothing', '10A7C342-CA82-48D4-8A38-46A2EB089B74', datetime.datetime(2002, 6, 1, 0, 0))
(4, None, 'Accessories', '2BE3BE36-D9A2-4EEE-B593-ED895D97C2A6', datetime.datetime(2002, 6, 1, 0, 0))
(5, 1, 'Mountain Bikes', '2D364ADE-264A-433C-B092-4FCBF3804E01', datetime.datetime(2002, 6, 1, 0, 0))
(6, 1, 'Road Bikes', '000310C0-BCC8-42C4-B0C3-45AE611AF06B', datetime.datetime(2002, 6, 1, 0, 0))
(7, 1, 'Touring Bikes', '02C5061D-ECDC-4274-B5F1-E91D76BC3F37', datetime.datetime(2002, 6, 1, 0, 0))
(8, 2, 'Handlebars', '3EF2C725-7135-4C85-9AE6-AE9A3BDD9283', datetime.datetime(2002, 6, 1, 0, 0))
(9, 2, 'Bottom Brackets', 'A9E54089-8A1E-4CF5-8646-E3801F685934', datetime.datetime(2002, 6, 1, 0, 0))
@phillipsj
phillipsj / Dockerfile
Last active October 16, 2020 18:35
A Dockerfile for restoring an Adventure Works databse on SQL Server for Linux.
# Adventure Works Database on SQL Server 2019
FROM mcr.microsoft.com/mssql/server:2019-CU5-ubuntu-18.04
# Note: This isn't a secure password, and please don't use this for production.
ENV SA_PASSWORD=ThisIsAReallyCoolPassword123
ENV ACCEPT_EULA=Y
# Setting the user
USER mssql
@phillipsj
phillipsj / Dockerfile
Last active April 27, 2022 16:29
A Dockerfile for downloading Adventure Works a database and restoring it.
# Adventure Works Database on SQL Server 2019
FROM mcr.microsoft.com/mssql/server:2019-CU5-ubuntu-18.04
# Note: This isn't a secure password, and please don't use this for production.
ENV SA_PASSWORD=ThisIsAReallyCoolPassword123
ENV ACCEPT_EULA=Y
# Change to root user to run wget and move the file
USER root
@phillipsj
phillipsj / Dockerfile
Created September 22, 2020 00:20
Just the SQL Server command.
CMD ["/opt/mssql/bin/sqlservr"]
@phillipsj
phillipsj / Dockerfile
Last active October 17, 2020 01:20
Run command for SQL Server restore.
# Launch SQL Server, confirm startup is complete, restore the database, then terminate SQL Server.
RUN ( /opt/mssql/bin/sqlservr & ) | grep -q "Service Broker manager has started" \
&& /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P ${SA_PASSWORD} -Q 'RESTORE DATABASE AdventureWorks2019 FROM DISK = "/var/opt/mssql/backup/AdventureWorks2019.bak" WITH MOVE "AdventureWorks2017" to "/var/opt/mssql/data/AdventureWorks2019.mdf", MOVE "AdventureWorks2017_Log" to "/var/opt/mssql/data/AdventureWorks2019_log.ldf", NOUNLOAD, STATS = 5' \
&& pkill sqlservr
@phillipsj
phillipsj / Dockerfile
Created September 22, 2020 00:22
Copy backup to restore command.
COPY AdventureWorks2019.bak /var/opt/mssql/backup/
@phillipsj
phillipsj / Dockerfile
Created September 22, 2020 00:23
Setting mssql user
# Setting the user
USER mssql
@phillipsj
phillipsj / Dockerfile
Created September 22, 2020 00:24
Setting environment variables for SQL Server
# Note: This isn't a secure password, and please don't use this for production.
ENV SA_PASSWORD=ThisIsAReallyCoolPassword123
ENV ACCEPT_EULA=Y