-
Notifications
You must be signed in to change notification settings - Fork 1
/
MysqlConnect.py
66 lines (54 loc) · 1.62 KB
/
MysqlConnect.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
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Mysql 连接操作
import pymysql
class Mysql(object):
def __init__(self):
self.config = {
"host": "127.0.0.1",
"user": "root",
"port": 3306,
"password": "root",
"db": "portscan",
}
def connect(self, config):
"""
连接mysql
"""
conn = pymysql.connect(**config)
return conn
def query(self, sql, argv=None):
"""
sql查询操作
"""
result = False
conn = self.connect(self.config)
try:
cursor = conn.cursor()
cursor.execute(sql, argv)
result = cursor.fetchall()
except Exception,e:
conn.rollback()
raise Exception(e)
conn.close()
return result
def execute(self, sql, argv=None):
"""
sql增删改操作
"""
result = False
conn = self.connect(self.config)
try:
cursor = conn.cursor()
result = cursor.execute(sql, argv)
conn.commit()
except Exception, e:
conn.rollback()
raise Exception(e)
conn.close()
return result
if __name__ == "__main__":
obj = Mysql()
insert_sql = 'insert into crawl_info(target,start_date,end_date,commandline,user_agent) value("http://www.0aa.me",1505117567,1505117569,"http://www.0aa.me","Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)")'
query_sql = 'select user()'
print(obj.execute(insert_sql))