forked from jpatokal/openflights
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_connector.py
More file actions
32 lines (27 loc) · 948 Bytes
/
database_connector.py
File metadata and controls
32 lines (27 loc) · 948 Bytes
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
import mysql.connector
class DatabaseConnector(object):
DB = 'flightdb2'
def __init__(self, args=None):
if args.local:
host = 'localhost'
pw = None
else:
host = '104.197.15.255'
with open('../sql/db.pw','r') as f:
pw = f.read().strip()
self.read_cnx = self.connect(host, pw)
self.cursor = self.read_cnx.cursor(dictionary=True)
self.write_cnx = self.connect(host, pw)
self.write_cursor = self.write_cnx.cursor(dictionary=True)
self.args = args
def connect(self, host, pw):
cnx = mysql.connector.connect(user='openflights', database=self.DB, host=host, password=pw)
cnx.raise_on_warnings = True
return cnx
def safe_execute(self, sql, params):
if self.args.live_run:
self.write_cursor.execute(sql, params, )
print ".. %s : %d rows updated" % (sql % params, self.write_cursor.rowcount)
self.write_cnx.commit()
else:
print sql % params