import csv import mysql.connector from tkinter import messagebox def collec(user,data_index): id='' with open('data.csv', 'r',encoding='utf-8', newline='') as file: reader = csv.reader(file) index=0 for row in reader: if index == data_index: id=row[0] index=index+1 db = mysql.connector.connect( host="localhost", user="root", passwd="A121023020309a", database="trial" ) sql='INSERT INTO trial.collections (user_id, essay_id) VALUES (%s, %s)' cursor=db.cursor() success='true' try: cursor.execute(sql, (user, id)) db.commit() except db.Error as e: print(f"An error occurred: {e}") db.rollback() messagebox.showinfo("Error", "收藏失败!") success='false' finally: # 确保cursor关闭,释放资源 cursor.close() if success == 'true': messagebox.showinfo('提示','收藏成功!')