-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbconnection.py
More file actions
68 lines (60 loc) · 2.08 KB
/
dbconnection.py
File metadata and controls
68 lines (60 loc) · 2.08 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
from os import environ
import pymysql
import logging
class Config:
db_user = environ.get('DATABASE_USERNAME')
db_password = environ.get('DATABASE_PASSWORD')
db_host = environ.get('DATABASE_HOST')
db_port = environ.get('DATABASE_PORT')
db_name = environ.get('DATABASE_NAME')
class Database:
def __init__(self, config):
self.host = config.db_host
self.username = config.db_user
self.password = config.db_password
self.port = config.db_port
self.dbname = config.db_name
self.conn = None
def open_connection(self):
try:
if self.conn is None:
self.conn = pymysql.connect(
host=self.host,
user=self.username,
passwd=self.password,
db=self.dbname,
connect_timeout=5
)
except pymysql.MySQLError as e:
logging.error(e)
raise e
finally:
logging.info('Connection opened')
def exec_query(self, query, args=None):
self.open_connection()
try:
with self.conn.cursor() as cur:
if 'SELECT' in query:
cur.execute(query, args)
result = cur.fetchall()
return [row for row in result]
else:
result = cur.execute(query, args)
self.conn.commit()
return f"{cur.rowcount} rows affected."
except pymysql.MySQLError as e:
print(e)
if self.conn:
self.conn.rollback()
finally:
if self.conn:
self.conn.close()
self.conn = None
logging.info('Connection closed')
# if __name__ == '__main__':
# config = Config()
# db = Database(config)
#
# print(db.exec_query('SELECT * from radio_info_tbl'))
#
# db.exec_query('insert into radio_info_tbl(create_time, wi_fi, ble, num_of_visitors) values(now(), %s, %s, %s)', ('{"MAC": "11:11:11"}', '{"MAC": "11:11:11"}', 10))