我在 python 中有一个列表,我目前将它插入到名为 SocketTest 的 SQL 表中。基本上我正在存储连接日志。我只想存储某人连接的最后一个位置 AP(接入点),而不是像我现在所做的那样存储他们的列表。它们按时间顺序读取并通过套接字,因此我在函数中获得的最新列表始终是该用户的最新列表。基本上我的程序从一个套接字读取,并用逗号分隔的信息制作一个列表,然后我将它插入到我的表中,虽然我的套接字中还有信息,但它不断重复。我在名为 i 的列表。我一直试图自己解决这个问题,但我没有经验。我想查看作为 ID 的 PK,如果尚未存储 ID,则插入该行,如果 ID 已存在,则替换或更新存储的行。我正在研究替换并插入重复的键,但无法让它工作。任何帮助表示赞赏,代码如下。
SocketTest 是表:PK 是 ID:Pringle 是一个示例行:
编辑:我将 MYSQLDB 作为 mdb 导入
def ParseArray(l): #parses line in socke
i.append(l.split()[+0] + '') # Gets Day
i.append(l.split()[+1] + '') # Gets Month
i.append(l.split()[+3] + '') # Gets Year
i.append(l.split()[+2] + '') # Gets Time
i.append(l.split()[-2] + '') # Gets Device
i.append(l.split()[+9] + '') # Gets ID
i.append(l.split()[+18] + '') # Gets AP
i.append(l.split()[+19] + '') # Gets AP Group
i.append(l.split()[+16] + '/n') # Gets MAC
#insert line into db else by primary key (ID)
#update line to db if ID doesn't exist
#pringle = ['Dec', '11', '2018', '15:10:51', 'iPhone', '[jeref2]',
# 'home', 'hm1', '45.88.34.58)\n']
sql = "INSERT INTO SocketTest (month, day, year, time, device, Id, ap,
ApGroup, MacAdd) VALUES ('%s');" % "', '".join(i)
cur.execute(sql)
con.commit()
泛舟湖上清波郎朗
相关分类