我有这个瓶子文件,它接收带有JSON文件的POST请求,该JSON文件将数据存储在数据库中。数据是有关连接到WIFI AP的设备的信息。然后使用模板将数据打印在网站上。打印后如何删除数据库的所有记录?这是一种检查设备是否断开连接的方法。
bottle.py
#!/usr/bin/env python
from bottle import route, run, template, request, SimpleTemplate, Bottle,abort,debug
import sqlite3
import json
import time
@route('/', method='POST')
def index():
body = request.body.read().decode('utf8') # read directly HTTP input
get_dict = json.loads(body) # decode json and get native python dict
maclist = get_dict.get('maclist')
signallist = get_dict.get('signallist')
data_list = list(zip(maclist, signallist))
conn = sqlite3.connect('db/users.db')
c = conn.cursor()
try:
c.executemany("INSERT INTO users (MAC,SIGNAL) VALUES(?,?)", data_list)
except Exception as exc:
c.executemany("REPLACE INTO users (MAC,SIGNAL) VALUES(?,?)", data_list)
conn.commit()
return "Items added."
@route('/data', method='GET')
def index():
conn = sqlite3.connect('db/users.db')
c = conn.cursor()
isEmpty = c.execute('SELECT count(*) from users')
if (isEmpty != 0):
c.execute('SELECT * FROM users')
return template('simple.tpl', rows = c.fetchall())
simple.tpl
<!DOCTYPE>
<html>
<head>
</head>
<body>
<table id="myTable">
<tr>
<td>MAC address</td>
<td>Signal</td>
</tr>
%for row in rows:
<tr>
<td>{{ row[1] }}</td>
<td>{{ row[2] }}</td>
</tr>
%end
</table>
</body>
</html>