-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd_utils.py
More file actions
41 lines (31 loc) · 796 Bytes
/
md_utils.py
File metadata and controls
41 lines (31 loc) · 796 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
33
34
35
36
37
38
39
40
41
import mysql.connector, atexit
from mysql.connector import errorcode
# Public Variables
config = {
"db_user": "moth_radio",
"db_password": "password", # DON'T COMMIT PASSWORDS
"db_database": "moth_radio"
}
# Private Variables
_db = None
# Public Methods
def db():
global _db
if _db: return _db
try:
db = mysql.connector.connect( user = config["db_user"],
password = config["db_password"],
host = "localhost",
database = config["db_database"])
_db = db
return _db
except mysql.connector.Error as error:
if error.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print "Database credentials incorrect; update config in md_utils."
else:
print error
# Private Methods
def _closeDb():
if _db: db().close()
# Cleanup
atexit.register(_closeDb)