From 71560782e35ec67e72c376e06ac089ab9e54f525 Mon Sep 17 00:00:00 2001 From: Saugat Acharya Date: Sun, 11 Feb 2024 19:13:48 +0545 Subject: [PATCH] Update examples to work with latest version of pyodbc --- examples/app-mssql/docker-compose.yml | 2 +- examples/app-mssql/main.py | 2 +- examples/app-mysql/main.py | 28 ++++++++++---------- examples/data-transfer-mssql/main.py | 2 +- examples/data-transfer-mysql/main.py | 38 +++++++++++++-------------- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/examples/app-mssql/docker-compose.yml b/examples/app-mssql/docker-compose.yml index d90bf5a..4b43c1a 100644 --- a/examples/app-mssql/docker-compose.yml +++ b/examples/app-mssql/docker-compose.yml @@ -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: diff --git a/examples/app-mssql/main.py b/examples/app-mssql/main.py index 3d1ca5a..3ceb676 100644 --- a/examples/app-mssql/main.py +++ b/examples/app-mssql/main.py @@ -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 ( diff --git a/examples/app-mysql/main.py b/examples/app-mysql/main.py index 1821ff7..7899a27 100644 --- a/examples/app-mysql/main.py +++ b/examples/app-mysql/main.py @@ -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) ) ''' @@ -31,27 +31,27 @@ 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() @@ -59,23 +59,23 @@ def setup_table(cur: pyodbc.Cursor, data: List): 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) diff --git a/examples/data-transfer-mssql/main.py b/examples/data-transfer-mssql/main.py index e0aa8bb..6e015da 100644 --- a/examples/data-transfer-mssql/main.py +++ b/examples/data-transfer-mssql/main.py @@ -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 diff --git a/examples/data-transfer-mysql/main.py b/examples/data-transfer-mysql/main.py index 12d294c..5418a19 100644 --- a/examples/data-transfer-mysql/main.py +++ b/examples/data-transfer-mysql/main.py @@ -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')) @@ -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')) @@ -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.')