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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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('提示','收藏成功!')