-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdb.py
More file actions
94 lines (81 loc) · 2.17 KB
/
db.py
File metadata and controls
94 lines (81 loc) · 2.17 KB
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
#!/usr/bin/env
#coding: utf-8
#################################################
# 作者:xiyang
# 时间:2015-05-14
# 版本:1.0
#################################################
import MySQLdb
from config import *
class DButils(object):
def __init__(self):
super(DButils, self).__init__()
#init mysql connection
self.conn= MySQLdb.connect(
host=db_host,
port = db_port,
user=db_user,
passwd=db_passwd,
connect_timeout=6000
)
self.cur = self.conn.cursor()
def fetchmany(self,sql):
try:
return self.cur.fetchmany(self.cur.execute(sql))
except Exception, e:
print e
print sql
def executemany(self,sql,values):
try:
self.cur.executemany(sql,values)
self.conn.commit()
except Exception, e:
print e
print sql
def fetchone(self,sql):
try:
self.cur.execute(sql)
return self.cur.fetchone()
except Exception, e:
print e
print sql
def execute(self,sql):
try:
self.cur.execute(sql)
self.conn.commit()
except Exception, e:
print e
print sql
def execute(self,sql,values):
try:
self.cur.execute(sql,values)
self.conn.commit()
except Exception, e:
print e
print sql
def create(self,sql):
try:
self.cur.execute(sql)
self.conn.commit()
except Exception, e:
print e
def is_table_exit(self,tableName):
show_sql = 'show tables;'
try:
return tableName in self.cur.fetchmany(self.cur.execute(show_sql))
except Exception,e:
print e
def close_db(self):
self.cur.close()
self.conn.close()
if __name__ == "__main__":
try:
db = DButils()
sql = "select * from qding_platform.order_base limit 1"
print sql
result = db.fetchmany(sql)
print result
for r in result:
print r
finally:
db.close_db()