forked from jcjohnson/torch-rnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_connect.py
47 lines (40 loc) · 1.33 KB
/
db_connect.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
"""db_connect.py:
Connect to database
"""
__author__ = "Dilawar Singh"
__copyright__ = "Copyright 2016, Dilawar Singh"
__credits__ = ["NCBS Bangalore"]
__license__ = "GNU GPL"
__version__ = "1.0.0"
__maintainer__ = "Dilawar Singh"
__email__ = "dilawars@ncbs.res.in"
__status__ = "Development"
import sys
import os
import mysql.connector
import mysql
try:
import ConfigParser
except ImportError as e:
import configparser as ConfigParser
config = ConfigParser.ConfigParser( )
thisdir = os.path.dirname( os.path.realpath( __file__ ) )
config.read( os.path.join( '/etc', 'hipporc' ) )
class MySQLCursorDict(mysql.connector.cursor.MySQLCursor):
def _row_to_python(self, rowdata, desc=None):
row = super(MySQLCursorDict, self)._row_to_python(rowdata, desc)
if row:
return dict(zip(self.column_names, row))
return None
user = config.get( 'mysql', 'user' )
host = config.get( 'mysql', 'host' )
passwd = config.get( 'mysql', 'password' ).replace( '"', '')
try:
db_ = mysql.connector.connect(
host = host
, user = user , password = passwd , db = 'hippo'
)
except Exception as e:
print( 'Could not connect for %s@%s -p%s' % (user, host, passwd) )
print( 'Error was %s' % e )
quit( )