Skip to content

Commit

Permalink
Update examples to work with latest version of pyodbc
Browse files Browse the repository at this point in the history
  • Loading branch information
mesaugat committed Feb 11, 2024
1 parent a0ebd1c commit 7156078
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion examples/app-mssql/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3.3"

services:
mssql:
image: "mcr.microsoft.com/mssql/server:2017-GA-ubuntu"
image: mcr.microsoft.com/mssql/server:latest
ports:
- "1433:1433"
environment:
Expand Down
2 changes: 1 addition & 1 deletion examples/app-mssql/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from faker import Faker


CONNECTION_STRING: str = 'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server};DATABASE={database};UID={username};PWD={password};'
CONNECTION_STRING: str = 'DRIVER={{ODBC Driver 18 for SQL Server}};SERVER={server};DATABASE={database};UID={username};PWD={password};TrustServerCertificate=yes;'

SQL_CREATE_TABLE: str = '''
CREATE TABLE users (
Expand Down
28 changes: 14 additions & 14 deletions examples/app-mysql/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from faker import Faker


CONNECTION_STR: str = 'DRIVER={{MySQL ODBC 8.0 Driver}};SERVER={server};DATABASE={database};UID={username};PWD={password};'
CONNECTION_STR: str = 'DRIVER={{MySQL ODBC 8.3 Unicode Driver}};SERVER={server};DATABASE={database};UID={username};PWD={password};'

SQL_CREATE_TABLE: str = '''
CREATE TABLE users (
id INT,
name VARCHAR(50),
id INT,
name VARCHAR(50),
city VARCHAR(50)
)
'''
Expand All @@ -31,51 +31,51 @@ def get_data(count: int) -> List[Tuple]:
return [row(i) for i in range(count)]


def connect_db() -> pyodbc.Connection:
def connect_db() -> pyodbc.Connection:
''' Connect to database. '''
print('Establishing mysql database connection.')
connection_str = CONNECTION_STR.format(
server=os.environ['DB_HOST'],
database=os.environ['DB_NAME'],
username=os.environ['DB_USER'],
server=os.environ['DB_HOST'],
database=os.environ['DB_NAME'],
username=os.environ['DB_USER'],
password=os.environ['DB_PASSWORD']
)

return pyodbc.connect(connection_str, timeout=300)


def setup_table(cur: pyodbc.Cursor, data: List):
def setup_table(cur: pyodbc.Cursor, data: List):
''' Create table and populate data. '''
print('Create a new table for users.')
cur.execute(SQL_CREATE_TABLE)
cur.commit()

print('Populate users data.')
for row in data:
for row in data:
cur.execute(SQL_INSERT_DATA, row)
cur.commit()


def fetch_data(cur: pyodbc.Cursor) -> List:
''' Fetch all data from the table. '''
print('List of data.')
cur.execute('SELECT * from users;')
cur.execute('SELECT * from users;')

return cur.fetchall()


def display_data(rows: List[Tuple[int, str, str]]):
def display_data(rows: List[Tuple[int, str, str]]):
template = '{:<5} {:<15} {:<10}'
print(template.format('ID', 'NAME', 'CITY'))
print('-' * 32)

for row in rows:
for row in rows:
print(template.format(row.id, row.name, row.city))


def main():
def main():
''' App entrypoint. '''
time.sleep(20) # wait for mysql database to fully spawn.
time.sleep(20) # wait for mysql database to fully spawn.
conn = connect_db()
cur = conn.cursor()
data = get_data(RECORD_COUNT)
Expand Down
2 changes: 1 addition & 1 deletion examples/data-transfer-mssql/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from faker import Faker


CONNECTION_STRING: str = 'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server};DATABASE={database};UID={username};PWD={password};'
CONNECTION_STRING: str = 'DRIVER={{ODBC Driver 18 for SQL Server}};SERVER={server};DATABASE={database};UID={username};PWD={password};TrustServerCertificate=yes;'

RECORD_COUNT: int = 10000

Expand Down
38 changes: 19 additions & 19 deletions examples/data-transfer-mysql/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@
import time
from typing import List, Tuple

import pyodbc
from faker import Faker
import pyodbc
from faker import Faker


CONNECTION_STRING: str = 'DRIVER={{MySQL ODBC 8.0 Driver}};SERVER={server};DATABASE={database};UID={username};PWD={password};'
CONNECTION_STRING: str = 'DRIVER={{MySQL ODBC 8.3 Unicode Driver}};SERVER={server};DATABASE={database};UID={username};PWD={password};'

RECORD_COUNT: int = 10000

SQL_INSERT_DATA: str = 'INSERT INTO users (id, name, city) VALUES (?, ?, ?);'


def main():
def main():
''' App entrypoint. '''
# Wait for mysql database server to fully spawn.
# Wait for mysql database server to fully spawn.
time.sleep(30)

source_db_conn, dest_db_conn = connect_to_databases()

with source_db_conn, dest_db_conn:
with source_db_conn, dest_db_conn:

source_db_cur = source_db_conn.cursor()
dest_db_cur = dest_db_conn.cursor()

with source_db_cur, dest_db_cur:
with source_db_cur, dest_db_cur:
print('Create users table and populate data in source database.')

source_db_cur.execute(extract_sql('sql/source_db_setup.sql'))
Expand All @@ -44,39 +44,39 @@ def main():
display_users(dest_db_cur)


def get_connection(host: str, db_name: str, db_user: str, db_password: str) -> pyodbc.Connection:
def get_connection(host: str, db_name: str, db_user: str, db_password: str) -> pyodbc.Connection:
''' Initiates and returns connection of a database. '''
print('Establishing mysql database connection to {host}.')
connection_str = CONNECTION_STRING.format(
server=host,
database=db_name,
server=host,
database=db_name,
username=db_user,
password=db_password
)

return pyodbc.connect(connection_str, timeout=300)


def connect_to_databases() -> Tuple:
''' Extract databases credentials from the environment and returns their connections. '''
source_db_conn = get_connection(
os.environ['SOURCE_DB_HOST'],
os.environ['SOURCE_DB_NAME'],
os.environ['SOURCE_DB_HOST'],
os.environ['SOURCE_DB_NAME'],
os.environ['SOURCE_DB_USERNAME'],
os.environ['SOURCE_DB_PASSWORD']
)

dest_db_conn = get_connection(
os.environ['DEST_DB_HOST'],
os.environ['DEST_DB_NAME'],
os.environ['DEST_DB_USERNAME'],
os.environ['DEST_DB_HOST'],
os.environ['DEST_DB_NAME'],
os.environ['DEST_DB_USERNAME'],
os.environ['DEST_DB_PASSWORD']
)

return source_db_conn, dest_db_conn


def populate_data(count: int, db_cursor: pyodbc.Cursor):
def populate_data(count: int, db_cursor: pyodbc.Cursor):
''' Generate user data. '''
fake = Faker()
row = lambda n: (n + 1, fake.format('name'), fake.format('city'))
Expand All @@ -95,7 +95,7 @@ def extract_sql(file: str) -> str:

def transfer_data(source_db_cursor: pyodbc.Cursor, dest_db_cursor: pyodbc.Cursor, dest_db_conn: pyodbc.Connection):
'''
Extracts users data from source database and
Extracts users data from source database and
stores them in destination database.
'''
print('Extracting users data from source database.')
Expand Down

0 comments on commit 7156078

Please sign in to comment.