/
momysql.py
53 lines (47 loc) · 1.8 KB
/
momysql.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
import MySQLdb
import conf
class MoMysql():
def __init__(self):
self.host = conf.getConfig("database","dbhost")
self.port = conf.getConfig("database","dbport")
self.database = conf.getConfig("database","dbname")
self.user = conf.getConfig("database","dbuser")
self.passwd = conf.getConfig("database","dbpassword")
self.charser = conf.getConfig("database","dbcharset")
#self.conn = MySQLdb.connect(self.host,self.database,self.user,self.passwd,self.charser)
#self.cursor = self.conn.cursor()
def moinsert(self):
db = MySQLdb.connect(host=str(self.host),port=int(self.port),db=self.database,
user=self.user,passwd=self.passwd,charset=self.charser)
cursor = db.cursor()
sql = "insert into groups(groupname) values(\"dbuser\")"
try:
cursor.execute(sql)
print "1234"
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
except:
print "INSERT FAILED!"
# Rollback in case there is any error
db.rollback()
# close mysql connection
db.close()
def moselect(self):
db = MySQLdb.connect(host=str(self.host), port=int(self.port), db=self.database,
user=self.user, passwd=self.passwd, charset=self.charser)
cursor = db.cursor()
sql = "select * from groups"
try:
# execute sql
cursor.execute(sql)
# get all records
dbresults = cursor.fetchall()
print "successed"
except:
print "Error: unable to fecth data"
# close the connection of database
db.close()
return dbresults;
momysql = MoMysql()
res = momysql.moselect()
print res