-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB.py
52 lines (45 loc) · 1.33 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
#-*- coding:utf-8 -*-
#Date:2017.9.21
import MySQLdb
class DB:
__host='localhost'
__db='paper'
__passwd='yush241263q'
__user='paperuser'
def connect(self):
try:
self.conn=MySQLdb.connect(host=self.__host,db=self.__db,passwd=self.__passwd,user=self.__user,charset='utf8')
except MySQLdb.Error,e:
print "Mysql Error %d:%s"%(e.args[0],e.args[1])
def Select(self,str):
if(self.__isset('self.conn')):
try:
self.cur=self.conn.cursor()
self.cur.execute(str)
return self.cur.fetchall()
except MySQLdb.Error, e:
print "Mysql Error %d:%s" % (e.args[0], e.args[1])
else:
return 0
def CurClose(self):
if(self.__isset('self.cur')):
self.cur.close()
def ConnClose(self):
if(self.__isset('self.conn')):
self.conn.close()
def Write(self,str):
if(self.__isset('self.conn')):
if(self.__isset('self.cur')==0):
self.cur=self.conn.cursor()
try:
self.cur.execute(str)
self.conn.commit()
except:
self.conn.rollback()
def __isset(self,v):
try:
type(eval(v))
except:
return 0
else:
return 1