我试图以 10 秒的间隔从 MySQL 数据库中提取数据。由于每秒传入的数据量是可变的,我计划每 10 秒提取最近的 50 行。我的第一个功能运行良好并适当地提取数据。
我的第二个函数应该从以前的拉取中删除重复项,并且只将唯一性推送到新变量,但是我遇到了一个错误,不知道如何修复它。似乎对于我的 if 语句,我不允许匹配每个元组的所有元素。我不确定如何改变这一点。
import mysql.connector
import datetime
import requests
from operator import itemgetter
import time
run = True
def sqlPull():
connection = mysql.connector.connect(user='XXXX', password='XXXXX', host='XXXXXX', database='MeshliumDB')
cursor = connection.cursor()
cursor.execute("SELECT TimeStamp, MAC, RSSI FROM wifiscan ORDER BY TimeStamp DESC LIMIT 50;")
data = cursor.fetchall()
connection.close()
time.sleep(10)
数据输出如下所示:
[(104346, datetime.datetime(2013, 11, 14, 17, 43, 20), u'00:1E:4C:03:C0:66', u' 16'), (104345, datetime.datetime(2013, 11, 14, 17, 43, 20), u'00:1E:4C:03:C0:66', u' 16'), (104344, datetime.datetime(2013, 11, 14, 17, 43, 19), u'00:26:AB:BB:FB:B8', u' 17'), (104343, datetime.datetime(2013, 11, 14, 17, 43, 18), u'8C:2D:AA:42:0E:67', u' 17'), (104342, datetime.datetime(2013, 11, 14, 17, 43, 17), u'E4:CE:8F:3F:36:E0', u' 13'), (104341, datetime.datetime(2013, 11, 14, 17, 43, 16), u'00:1E:8F:75:82:35', u' 9'), (104340, datetime.datetime(2013, 11, 14, 17, 43, 16), u'00:1E:8F:75:82:35', u' 9'), (104339, datetime.datetime(2013, 11, 14, 17, 43, 15), u'74:44:01:31:D9:E2', u' 9'), (104338, datetime.datetime(2013, 11, 14, 17, 43, 19), u'84:38:35:5E:F8:BA', u' 8'), (104337, datetime.datetime(2013, 11, 14, 17, 43, 15), u'00:26:08:E2:38:F9', u' 8')]
def dupCatch():
lastPull = []
for TimeStamp, MAC, RSSI in data:
if TimeStamp, MAC, RSSI not in lastPull
data = newData
else
lastPull = data
while run == True:
sqlPull()
dupCatch()
print newData
File "<ipython-input-1-6d9d5eb980c9>", line 20
if TimeStamp, MAC, RSSI not in lastPull
^
SyntaxError: invalid syntax