-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_util.py
59 lines (46 loc) · 1.41 KB
/
mysql_util.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
import MySQLdb
# MySQL 建立连接
class MySQLPipeline(object):
def __init__(self, host, port, user, password, db):
self.mysql_host = host
self.mysql_port = port
self.mysql_user = user
self.mysql_password = password
self.mysql_db = db
# 创建MYSQL数据库链接对象
self.conn = MySQLdb.connect(host=self.mysql_host,
user=self.mysql_user,
password=self.mysql_password,
db=self.mysql_db,
charset="utf8")
# 查询数据
def searching(self, sql):
try:
with self.conn as cur:
cur.execute(sql)
logger.info("sql查询成功")
return cur
except Exception as e:
print(e)
logger.error(e)
return None
# 增删改
def processing(self, sql):
try:
with self.conn as cur:
cur.execute(sql)
logger.info("sql处理成功")
except Exception as e:
print(e)
logger.error(e)
return None
if __name__ == '__main__':
# def get_results():
# result = a.searching(query_sql)
#
# if result:
# row = [row for row in result]
# word, id = row[0][0], row[0][1]
# return word, id
# else:
# print("查询为空")