forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmysql.py
More file actions
167 lines (147 loc) · 3.88 KB
/
mysql.py
File metadata and controls
167 lines (147 loc) · 3.88 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# coding=utf-8
__author__ = 'zhanghe'
from MySQLdb import *
# 本地环境
db_config_local = {
'host': '192.168.1.1',
'user': 'xxxx',
'passwd': 'xxxxxxxx',
'db': '',
'port': 3306
}
# 线上环境
db_config_online = {
'host': '192.168.1.2',
'user': 'xxxx',
'passwd': 'xxxxxxxx',
'db': '',
'port': 3306
}
class Mysql(object):
"""
自定义mysql工具
"""
def __init__(self, db_config, db_name=None):
self.db_config = db_config
if db_name is not None:
self.db_config['db'] = db_name
try:
self.conn = Connection(
self.db_config['host'],
self.db_config['user'],
self.db_config['passwd'],
self.db_config['db'],
self.db_config['port']
)
except Exception, e:
print e
def is_conn_open(self):
"""
检测连接是否打开
:return:
"""
if self.conn is None or self.conn.open == 0:
return False
else:
return True
def close_conn(self):
"""
关闭数据库连接
:return:
"""
if self.is_conn_open() is True:
self.conn.close()
def get_row(self, table_name=None, condition=None):
"""
获取单行数据
:return:
"""
if self.is_conn_open() is False:
print '连接已断开'
return None
try:
cursor = self.conn.cursor()
cursor.execute("set NAMES UTF8") # 解决乱码
if condition is not None:
sql_condition = 'where '
sql_condition += ' and '.join(condition)
else:
sql_condition = ''
sql = 'select * from %s %s limit 1' % (table_name, sql_condition)
print sql
cursor.execute(sql)
row = cursor.fetchone()
if row is None:
print None
return None
else:
# print type(row)
if '??' in row[1]:
print '存在乱码'
print '%s\t%s' % (row[0], row[1])
# print '%s\t%s' % (type(row[0]), type(row[1]))
cursor.close()
return row
except Exception, e:
print e
def get_count(self, table_name=None):
"""
获取记录总数
:return:
"""
if self.is_conn_open() is False:
print '连接已断开'
return None
if table_name is None:
print 0
return 0
try:
cursor = self.conn.cursor()
sql = 'select count(*) from %s' % table_name
cursor.execute(sql)
row = cursor.fetchone()
count = row[0]
cursor.close()
print count
return count
except Exception, e:
print e
def test():
"""
测试Mysql类
:return:
"""
# 实例化db_company库的连接
mysql_company = Mysql(db_config_local, 'db_company')
# 查询总数
count = mysql_company.get_count('company')
print '总记录数:%s' % count
# 关闭数据库连接
# mysql_company.close_conn()
# 查询单条记录
row = mysql_company.get_row('company', ['cid=17'])
if row is not None:
print '单条记录:%s\t%s' % (row[0], row[1])
else:
print '没有记录'
# 关闭数据库连接(测试再次关闭)
mysql_company.close_conn()
if __name__ == '__main__':
test()
"""
测试结果:
1748901
总记录数:1748901
select * from company where cid=18 limit 1
None
没有记录
1748901
总记录数:1748901
连接已断开
没有记录
1748901
总记录数:1748901
select * from company where cid=17 limit 1
17 四川XXX有限责任公司
单条记录:17 四川XXX有限责任公司
"""