Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf9f41d

Browse files
authored
Create dodb.py
python操作db的原生sql
1 parent 141dd96 commit cf9f41d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

dodb.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
#_*_ coding:utf-8 _*_
3+
4+
import MySQLdb
5+
6+
7+
with open('/var/logs/install.log') as f:
8+
a = f.read().strip().split('\n')
9+
hostl = []
10+
for i in a:
11+
b = i.split('\t')
12+
if b[3] == 'stop':
13+
hostl.append(b[1])
14+
else:
15+
pass
16+
for host in list(set(hostl)):
17+
conn= MySQLdb.connect(
18+
host='localhost',
19+
port = 3306,
20+
user='root',
21+
passwd='111111',
22+
db ='pj_install',
23+
)
24+
try:
25+
#select
26+
sql = "select * from automsg where HostName='{0}'".format(host)
27+
cur = conn.cursor()
28+
cur.execute(sql)
29+
ret = cur.fetchone()
30+
31+
#update
32+
update_sql = "update installretmsg set status=2, msg=u'安装成功!' where id={0}".format(int(ret[0]))
33+
cur.execute(update_sql)
34+
35+
cur.close()
36+
conn.commit()
37+
conn.close()
38+
except:
39+
pass

0 commit comments

Comments
 (0)