Skip to content

Commit

Permalink
Merge pull request #9 from v1shwa/feature/mssql-example
Browse files Browse the repository at this point in the history
Setup an example project using mssql and pyodbc
  • Loading branch information
kabirbaidhya authored Oct 7, 2019
2 parents 5217e9e + 615d530 commit c5942d9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/app-mssql/Dockerfile
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"]
16 changes: 16 additions & 0 deletions examples/app-mssql/README.md
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
```
19 changes: 19 additions & 0 deletions examples/app-mssql/docker-compose.yml
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}
40 changes: 40 additions & 0 deletions examples/app-mssql/main.py
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)

0 comments on commit c5942d9

Please sign in to comment.