You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1004 B

6 months ago
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':
6 months ago
messagebox.showinfo('提示','收藏成功!')