-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
78 lines (64 loc) · 2.01 KB
/
db.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# -*- coding: utf8 -*-
"""
SLEM Mydb Class
"""
__author__ = "Arnis Civciss (arnis.civciss@gmail.com)"
__version__ = "$Revision: 0.1 $"
__date__ = "$Date: 2012/08/09 $"
__copyright__ = "Copyright (c) 2012 Arnis Civciss"
__license__ = ""
import sys
import MySQLdb as mdb
class Mydb:
"""Class Db implemets methods to access SLEM Mysql data base."""
def __init__(self, **kwargs):
"""Init - host, user, passwd, dbname - params."""
self.host = kwargs['host']
self.user = kwargs['user']
self.passwd = kwargs['passwd']
self.dbname = kwargs['dbname']
def connect(self):
"""Connects and creates cursor and db handler."""
self.dbh = mdb.connect(self.host,self.user,self.passwd,self.dbname)
self.cursor = self.dbh.cursor(mdb.cursors.DictCursor)
def do_sql(self, sql, sql_data=None, get='get'):
'''Executes sql statement
if get=='get' selects and returns rows and rowcount.
if get=='set' does executemany. sql_data is a tuple of parameters.
e.g.
sql = """INSERT IGNORE INTO dslamz_dslamtype (type) VALUES (%s)"""
sql_data = [("ETH"),("ATM")]
try:
la = mango.do_sql(sql, sql_data, 'set')
except mango.Error(), e:
print "Db Error %d: %s" % (e.args[0], e.args[1])
'''
if get == 'get':
self.cursor.execute(sql)
rows = self.cursor.fetchall()
rowcount = self.cursor.rowcount
return rows, rowcount
elif get == 'set':
self.cursor.executemany(sql, sql_data)
self.dbh.commit ()
return 1
else: pass
def Error(self):
"""Error handling"""
return mdb.Error
def close(self):
"""Close connection."""
self.dbh.close()
def get_host(self):
"""returns host attribute"""
return self.host
def get_user(self):
"""returns user attribute"""
return self.user
def get_dbname(self):
"""returns dbname attribute"""
return self.dbname
def get_passwd(self):
"""returns passwd attribute"""
return self.passwd