I have a list in python that I'm currently inserting into a SQL table called SocketTest. Basically I'm storing connection logs. I want to just store the last location AP (access point) someone connected to instead of a list of them like I'm doing now. They read and come through the socket in chronological order so the newest list I get in my function is always the most current one for that user. Basically my program reads from a socket, and makes a list with the information separated by commas, then I insert it into my table and while I still have information in the socket it keeps getting repeated. My list in named i. I've been trying to figure this out myself but I'm not to experienced. I would like to look at the PK which is the ID and insert the line if no ID is stored yet and replace or update the stored row if the ID already exists. I was looking into replace into and insert on duplicate key but couldn't get it to work. Any help is appreciated, code is below.
SocketTest is the Table: PK is the ID: Pringle is an example line:
EDIT: I'm importing MYSQLDB as 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()
Edit: Rest of the code
#!/bin/python
import socket
import os, os.path
import MySQLdb as mdb
con = mdb.connect('x', 'x', 'x', 'x');
cur = con.cursor()
#pringle=['Dec', '11', '2018', '15:10:51', 'iPhone', '[josheer]', 'FRD', 'BHJ', '45.33.88.34)\n']
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
try:
row_to_insert = [val1, val2, val3]
cur.execute("INSERT INTO SocketTest (month, day, year, time, device,Id, ap, ApGroup, MacAdd) VALUES(%s, %s, %s)",
(i)
except: pymysql.IntegrityError:
cur.execute("""UPDATE SocketTest
SET column2 = {1}, column3 = {2}
WHERE column1 = {0}
""".format(val1, val2, val3)
#sql = "REPLACE INTO SocketTest (month, day, year, time, device,Id, ap, ApGroup, MacAdd) VALUES ('%s');" % "', '".join(
#i)
#cur.execute(sql)
#con.commit()
con.commit()
print(i)
del i[:]
i = []
if os.path.exists("/home/log/x"):
os.remove("/home/log/x")
sock = socket.socket(x, sx)
sock.bind("/home/log/xt")
infile = sock.makefile('r')
while True:
l = sock.recv(4096).decode()
ParseArray(l)