I'm making a login page code on Python (with Tkinter) that creates a database table where the users' id and the time they logged in are registered whenever a user succesfully logs in. However, everytime I run it, I keep getting the same error, and I'm not sure if it's because I'm using "Try Except" incorrectly or the way I'm inserting the foreign primery key in cur.execute(insert_query, ...) Here's the block of code of the .py file that I worked on where I'm encountering the most issues:
def login():
if idEntry.get() == '' or passwdEntry.get() == '':
messagebox.showerror('Alert', 'Please enter all entry fields!')
else:
db = pymysql.connect(host='127.0.0.1', user='root', password='Orlando1208', database='testP')
cur = db.cursor()
query = 'select * from personaldata where passwrd=%s'
cur.execute(query, (passwdEntry.get(),))
roles = cur.fetchone()
if roles == None:
messagebox.showerror('Alert!', 'Incorrect username or password')
return
else:
try:
queryActi = 'use testP'
cur.execute(queryActi)
queryActi='create table if not exists Registro_Actividad (userId INT PRIMARY KEY NOT NULL,fecha DATETIME,FOREIGN KEY (userId) REFERENCES personaldata(id)) ' \
cur.execute(queryActi)
except:
loginTime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cur.execute('use testP')
insert_query = 'insert into Registro_Actividad (userId, fecha) VALUES (%s, %s)'
last_id = cur.lastrowid
cur.execute(insert_query, (last_id,) + loginTime)
db.commit()
db.close
messagebox.showinfo('success', 'Login Successful')
# clear screen
idEntry.delete(0, END)
passwdEntry.delete(0, END)
main_running = False
for proc in psutil.process_iter():
if 'Mainwin.py' in proc.name():
main_running = True
break
if not main_running:
subprocess.Popen(['python', 'Mainwin.py'])
windows.destroy()
An advice on how to fix this?