-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperat_json.py
More file actions
48 lines (42 loc) · 1.11 KB
/
Copy pathoperat_json.py
File metadata and controls
48 lines (42 loc) · 1.11 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
#coding: utf-8
import MySQLdb
import json
#connect db
db = MySQLdb.connect("192.168.0.107", "admin_zone", "xxxxxx", "detection")
#get vernier
cursor = db.cursor()
#sql statement
db_sql = """
CREATE TABLE iptable (
user char(20) not null,
job char(20) not null,
mac char(48) not null,
ip char(64) not null,
description char(128)
)
"""
#execute sql
data = cursor.execute( """
show tables like 'iptable'
""")
#if db not exists, create db, else, insert sql statement
try:
if data == 0:
cursor.execute(db_sql)
print "db is create"
except:
print "db is exist, the data will insert db"
#dynamically inserted into the datebase
def operate(operation):
operate_json = operation
json_str = json.dumps(operate_json)
date_encode = json_str.encode('utf-8')
value = eval(date_encode)
sql = """INSERT INTO iptable VALUES (%(mac)s, %(ip)s)"""
try:
cursor.execute(sql, value)
db.commit()
print 'success json has been insert db'
except:
db.rollback()
print 'error, please check your operat'