-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.py
More file actions
59 lines (45 loc) · 1.27 KB
/
sql.py
File metadata and controls
59 lines (45 loc) · 1.27 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
# coding=UTF-8
import MySQLdb
import json
import time
def SQL_func(data):
localhost = ""
username = ""
passwd = ""
DBName = ""
json_data = data
try:
decoded = json.loads(json_data)
# pretty printing of json-formatted string
print json.dumps(decoded, sort_keys=True, indent=4)
print "JSON parsing example: ", decoded['name']
name = decoded['name']
type_log = decoded['type']
print type_log
except (ValueError, KeyError, TypeError):
print "JSON format error"
try:
# build DB connection information and set encoding to utf-8
db = MySQLdb.connect(localhost, username, passwd, DBName, charset='utf8')
cursor = db.cursor()
cursor.execute("SELECT * FROM log")
sql = "INSERT INTO log (device_name, date, log) VALUES (%s, %s, %s)"
insert_data = (name, time.strftime('%d %b %Y %H:%M:%S'), type_log)
# execute SQL statement
cursor.execute(sql, insert_data)
db.commit()
"""
# get multiple data
results = cursor.fetchall()
# loop for getting data
for record in results:
col1 = record[0]
col2 = record[1]
col3 = record[2]
col4 = record[3]
print "%s, %s, %s, %s" % (col1, col2, col3, col4)
"""
# close connection
db.close()
except MySQLdb.Error as e:
print "Error %d: %s" % (e.args[0], e.args[1])