add_existing_pdf.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import pymysql
  2. # Database connection
  3. DB_CONFIG = {
  4. "host": "rm-f8ze60yirdj8786u2wo.mysql.rds.aliyuncs.com",
  5. "user": "root",
  6. "password": "csqz@20255",
  7. "db": "csqz-client",
  8. "charset": "utf8mb4",
  9. "cursorclass": pymysql.cursors.DictCursor
  10. }
  11. def add_existing_pdf():
  12. """Add existing PDF to genealogy_pdfs table"""
  13. conn = pymysql.connect(**DB_CONFIG)
  14. try:
  15. with conn.cursor() as cursor:
  16. # Check if the PDF already exists
  17. cursor.execute("SELECT * FROM genealogy_pdfs WHERE file_name LIKE %s", ("%留总-正式-卷四齿录%",))
  18. existing = cursor.fetchone()
  19. if existing:
  20. print("PDF already exists in the database")
  21. return
  22. # Add the PDF entry
  23. pdf_file_name = "留总-正式-卷四齿录_留总收藏.pdf"
  24. # Create a placeholder URL based on the image URLs
  25. placeholder_url = "https://file.chunsunqiuzhu.com/data/2026/03/31/留总-正式-卷四齿录_留总收藏.pdf"
  26. uploader = "留总"
  27. cursor.execute(
  28. "INSERT INTO genealogy_pdfs (file_name, oss_url, uploader) VALUES (%s, %s, %s)",
  29. (pdf_file_name, placeholder_url, uploader)
  30. )
  31. conn.commit()
  32. print(f"PDF '{pdf_file_name}' added to genealogy_pdfs table")
  33. except Exception as e:
  34. print(f"Error: {e}")
  35. conn.rollback()
  36. finally:
  37. conn.close()
  38. if __name__ == "__main__":
  39. add_existing_pdf()