-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreateSchema.py
72 lines (53 loc) · 1.84 KB
/
createSchema.py
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 pyodbc
import os
def connect_to_iris():
connection = get_connection_config()
# Connect to InterSystems IRIS using PyODBC
connection_string = 'DRIVER={};SERVER={};PORT={};DATABASE={};UID={};PWD={}'\
.format(connection['driver'], connection['ip'], int(connection['port']),\
connection['namespace'], connection['username'], connection['password'])
pyodbc_connection = pyodbc.connect(connection_string)
pyodbc_connection.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
pyodbc_connection.setencoding(encoding='utf-8')
print("Connected to InterSystems IRIS")
return pyodbc_connection
def get_connection_config():
with open("../connections.config") as f:
f = f.read()
f = f.strip()
f = f.split("\n")
dict = {}
for line in f:
line = line.split(":")
dict[line[0].strip()] = line[1].strip()
return dict
# Remove old table if exist
def delete_old_table(cursor, table_name):
drop_table = "DROP TABLE {}".format(table_name)
cursor.execute(drop_table)
def create_employee(connection):
# Create cursor
cursor = connection.cursor()
# Uncomment the code below:
# create_employee = """
# CREATE TABLE Demo.Employee(
# ID Integer PRIMARY KEY AUTO_INCREMENT,
# Name varchar(256),
# Title varchar(256),
# Department varchar(50)
# )
# """
try:
cursor.execute(create_employee)
except Exception as e:
delete_old_table(cursor, "Demo.Employee")
print(e)
connection.commit()
print("Created table Demo.Employee successfully")
def run():
# Get connections for PyODBC
pyodbc_connection = connect_to_iris()
# Populate and retrieve data using PyODBC
create_employee(pyodbc_connection)
if __name__ == '__main__':
run()