-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_mysql.py
102 lines (84 loc) · 2.23 KB
/
py_mysql.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#codeing=utf-8
import MySQLdb as mdb
class py_mysql:
host=""
port=""
user=""
password=""
dbname=""
conn=""
cursor=""
def __init__(self,host,port,dbname,user,password):
self.host=host
self.port=port
self.dbname=dbname
self.user=user
self.password=password
def mysql_connect(self,ismaster=0):
self.conn = mdb.connect(host=self.host,port=self.port,user=self.user,passwd=self.password)
self.cursor=self.conn.cursor()
self.conn.select_db(self.dbname)
def execute(self,sql):
return self.cursor.execute(sql)
def query(self,sql):
try:
self.cursor.execute(sql)
return self.cursor.fetchall()
except mdb.Error,e:
print "Error %d:%s" % (e.args[0],e.args[1])
def insert(self,tablename,dataarr):
try:
sql = self.dealinsertdata(tablename,dataarr)
re.self.execute(sql)
return re
except mdb.Error,e:
print "Erro %d:%s" % (e.args[0],e.args[1])
def update(self,tablename,dataarr,where):
try:
sql = self.dealupdatedata(tablename,dataarr,where)
re = self.execute(sql)
return re
except mdb.Error,e:
print "Error %d:%s" % (e.args[0],e.args[1])
def delete(self,tablename,where):
try:
sql = "DELETE FROM " + tablename + " WHERE " + where
re = self.execute(sql)
return re
except mcb.Error,e:
print "Error %d:%s" % (e.args[0],e.args[1])
def insert_id(self):
return self.conn.insert_id()
def affected_rows(self):
return self.cursor.rowcount
def dealinsertdata(self,tablename,data):
try:
key=[]
val=[]
for i in range(len(data)):
key.append("'" + data.keys()[i] + "'")
k = data.keys()[i]
val.append("'" + data[k] + "'")
field=",".join(key)
vals=",".join(val)
sql="INSERT INTO " + tablename + "(" + field + ") VALUES (" + vals + ")"
return sql
except:
print "array to string error"
def dealupdatedata(self,tablename,data,condition):
try:
val=[]
updatestr = ""
for i in range(len(data)):
k = data.keys()[i]
val.append("'" + data.keys()[i] + "' = "+ "'" + data[k] + "'")
vals=",".join(val)
sql="UPDATE " + tablename + " SET " + vals + " where " + condition
return sql
except:
print " array to string error"
def mysql_close(self):
self.conn.close()
self.cursor.close()
def __del__(self):
self.mysql_close()