Repository navigation
Expand file tree
/
Copy pathdatabase connections PythonAPI.py
More file actions
72 lines (67 loc) · 3.03 KB
/
Copy pathdatabase connections PythonAPI.py
File metadata and controls
72 lines (67 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Import libraries required for connecting to mysql
import mysql.connector
# Import libraries required for connecting to DB2 or PostgreSql
import psycopg2
# Connect to MySQL
# Connect to DB2 or PostgreSql
# Find out the last rowid from DB2 data warehouse or PostgreSql data warehouse
# The function get_last_rowid must return the last rowid of the table sales_data on the IBM DB2 database or PostgreSql.
def get_last_rowid():
dsn_hostname = '127.0.0.1'
dsn_user='postgres' # e.g. "abc12345"
dsn_pwd ='MTM5MzMtbGFrc2ht' # e.g. "7dBZ3wWt9XN6$o0J"
dsn_port ="5432" # e.g. "50000"
dsn_database ="postgres" # i.e. "BLUDB"
# create connection
conn = psycopg2.connect(
database=dsn_database,
user=dsn_user,
password=dsn_pwd,
host=dsn_hostname,
port= dsn_port)
#Crreate a cursor onject using cursor() method
cursor = conn.cursor()
# create table
SQL = """ select rowid from sales order by rowid desc limit 5 """
# Execute the SQL statement
cursor.execute(SQL)
print("got last row id")
last_row_id = get_last_rowid()
print("Last row id on production datawarehouse = ", last_row_id)
pass
# List out all records in MySQL database with rowid greater than the one on the Data warehouse
# The function get_latest_records must return a list of all records that have a rowid greater than the last_row_id in the sales_data table in the sales database on the MySQL staging data warehouse.
def get_latest_records(rowid):
connection = mysql.connector.connect(user='root', password='MTg0NDgtbmFnaWF0',host='127.0.0.1',database='sales')
cursor = connection.cursor
cursor.execute("SELECT * FROM sales WHERE rowid > %s", (rowid,)) # Fetch records with rowid > last_row_id
new_records = cursor.fetchall()
return new_records # Return the list of new records
print("New rows on staging datawarehouse = ", len(new_records))
pass
# Insert the additional records from MySQL into DB2 or PostgreSql data warehouse.
# The function insert_records must insert all the records passed to it into the sales_data table in IBM DB2 database or PostgreSql.
def insert_records(records):
dsn_hostname = '127.0.0.1'
dsn_user='postgres' # e.g. "abc12345"
dsn_pwd ='MTM5MzMtbGFrc2ht' # e.g. "7dBZ3wWt9XN6$o0J"
dsn_port ="5432" # e.g. "50000"
dsn_database ="postgres" # i.e. "BLUDB"
# create connection
conn = psycopg2.connect(
database=dsn_database,
user=dsn_user,
password=dsn_pwd,
host=dsn_hostname,
port= dsn_port)
cursor = conn.cursor()
for row in records: # Iterate through the actual new_records list
cursor.execute("INSERT INTO sales_data(rowid, product, category) VALUES(%s, %s, %s)", row)
conn.commit()
insert_records([(5,'Mobile','Electronics'),(6,'Mobile','Electronics')])
print("New rows inserted into production datawarehouse = ", len(new_records))
# disconnect from mysql warehouse
connection.close()
# disconnect from DB2 or PostgreSql data warehouse
conn.close()
# End of program