forked from iamadamhair/ispy_python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
44 lines (35 loc) · 851 Bytes
/
database.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
import logging as log
connection = None
cursor = None
driver = None
def connect(address, username, password, database, unix_socket='/tmp/mysql.sock'):
"""
Connect to the database
"""
global connection
global cursor
global driver
connection = driver.connect(address, username, password, database, unix_socket=unix_socket)
with connection:
cursor = connection.cursor()
log.info('Connected to database')
def init_driver():
"""
Use native MySQLdb driver, or fall back on pymysql
Must be called before connect()
"""
global driver
try:
import MySQLdb
log.info('Using MySQLdb')
driver = MySQLdb
return MySQLdb
except ImportError:
log.info('MySQLdb not found')
try:
import pymysql
log.info('Using pymysql')
driver = pymysql
return pymysql
except ImportError:
raise Exception('No database driver found')