-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from v1shwa/feature/mssql-example
Setup an example project using mssql and pyodbc
- Loading branch information
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FROM laudio/pyodbc:1.0.8 | ||
LABEL author="Vishwa" | ||
|
||
WORKDIR /app | ||
|
||
# Copy code to container | ||
COPY . . | ||
|
||
CMD ["python", "main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# MSSQL Example | ||
Example project using mssql with pyodbc docker image. | ||
|
||
### Usage | ||
|
||
- Set credentials for the DB in enviromental variables. | ||
``` | ||
export DB_NAME='tempdb'; | ||
export DB_USERNAME='SA'; | ||
export DB_PASSWORD='someP4ssword'; | ||
``` | ||
- Start the containers | ||
|
||
``` | ||
docker-compose up | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
version: '3.3' | ||
|
||
services: | ||
mssql_server: | ||
image: "mcr.microsoft.com/mssql/server:2017-GA-ubuntu" | ||
ports: | ||
- "1433:1433" | ||
environment: | ||
ACCEPT_EULA: Y | ||
SA_PASSWORD: ${DB_PASSWORD} | ||
mssql_example_project: | ||
build: . | ||
depends_on: | ||
- mssql_server | ||
environment: | ||
DB_HOST: mssql_server | ||
DB_NAME: ${DB_NAME} | ||
DB_USERNAME: ${DB_USERNAME} | ||
DB_PASSWORD: ${DB_PASSWORD} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import sys | ||
import os | ||
import time | ||
import pyodbc | ||
|
||
# waiting for mssql db server to fully spawn | ||
time.sleep(3) | ||
|
||
print("Creating a mssql connection") | ||
connection_str = 'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server};DATABASE={database};UID={username};PWD={password};'.format( | ||
server=os.environ['DB_HOST'], | ||
database=os.environ['DB_NAME'], | ||
username=os.environ['DB_USERNAME'], | ||
password=os.environ['DB_PASSWORD'] | ||
) | ||
conn = pyodbc.connect(connection_str, timeout=300) | ||
cur = conn.cursor() | ||
|
||
|
||
print("Create a new table: fruits") | ||
cur.execute("CREATE TABLE fruits (id INT, name NVARCHAR(50), quantity INT);") | ||
conn.commit() | ||
|
||
print("Insert data into `fruits` table") | ||
cur.execute("INSERT INTO fruits VALUES (1, 'banana', 150);") | ||
cur.execute("INSERT INTO fruits VALUES (2, 'orange', 64);") | ||
cur.execute("INSERT INTO fruits VALUES (2, 'apples', 35);") | ||
conn.commit() | ||
|
||
print("Select data from `fruits` table") | ||
cur.execute("SELECT * FROM fruits WHERE quantity > 50;") | ||
rows = cur.fetchall() | ||
for row in rows: | ||
print(row.id, row.name, row.quantity) | ||
|
||
print("Closing the connection") | ||
cur.close() | ||
conn.close() | ||
|
||
sys.exit(0) |