-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoaderData.py
85 lines (65 loc) · 2.17 KB
/
LoaderData.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
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import informixdb
class DataLoaderTool(object):
def __init__(self):
self.Database=os.getenv('DB_CONFIGURE_DB')
self.Server=os.getenv('INFORMIXSERVER')
self.Username=os.getenv('DB_CONFIGURE_USERNAME')
self.Password=os.getenv('DB_CONFIGURE_PASSWORD')
self.weather_station_data_file = '/home/opuser/sample/data/weather_station.sql'
self.temperature_monitor_data_file = '/home/opuser/sample/data/temperature_monitor.sql'
def resetDB(self):
self.clearDB()
self.initDB()
def initDB(self):
self.weather_station()
self.temperature_monitor()
def weather_station(self):
self.load_data(self.weather_station_data_file)
def temperature_monitor(self):
self.load_data(self.temperature_monitor_data_file)
def load_data(self, data_file):
try:
conn = None
conn = self.connct_ifx()
count = 0
for line in open(data_file):
count += 1
self.execute_sql(line, conn)
print 'successfully inserted %d rows' % count
finally:
if conn:
conn.close()
def clearDB(self):
try:
conn = None
conn = self.connct_ifx()
drop_weather_station = "drop table if exists weather_station;"
drop_temperature_monitor = "drop table if exists temperature_monitor;"
self.execute_sql(drop_weather_station, conn)
self.execute_sql(drop_temperature_monitor, conn)
print 'successfully cleared DB'
finally:
if conn:
conn.close()
def connct_ifx(self):
try:
conn = None
conn = informixdb.connect(self.Database+'@'+self.Server,self.Username,self.Password)
if not conn:
raise Exception("Failed to connect via SQL to " + self.Database+'@'+self.Server,self.Username,self.Password)
else:
print("connect !!!!")
return conn
except:
pass
def execute_sql(self, sql, conn):
try:
cur = None
cur = conn.cursor()
cur.execute(sql)
conn.commit()
print 'execute',sql,'successfully!!'
finally:
if cur:
cur.close()