-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDBConnection.py
51 lines (41 loc) · 1.35 KB
/
DBConnection.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
48
49
50
51
'''
Created on 09.10.2011
The sole purpose of this class is to return an
open database connection.
@author: Kamil Wozniak
'''
import sqlite3
import ConfigParser
try:
import MySQLdb
mysqlModExists = True
except ImportError:
mysqlModExists = False
class DBConnection(object):
'''
Returns a database connection object, which can
be used to execute SQL-code.
'''
def getDBConnection(self):
config = ConfigParser.RawConfigParser()
config.read('wsn.cfg')
type = config.get('Database-Config', 'type')
if type == 'sqlite3':
conn = sqlite3.connect("data.db")
conn.text_factory = str
return conn
if type == 'mysql':
if mysqlModExists == False:
print('MySQLdb module can not be found. Read the README.txt')
else:
host = config.get('Database-Config', 'host')
user = config.get('Database-Config', 'user')
password = config.get('Database-Config', 'password')
dbname = config.get('Database-Config', 'dbname')
conn = MySQLdb.connect(host, user, password, dbname)
conn.text_factory = str
return conn
def __init__(self):
'''
Constructor
'''