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.
# 教育机构 :山东科技大学
# 学生 :石廷达
# 开发日期 :2024/12/28
importpymysql
# 连接数据库的函数
defconnect_db():
# 先连接 MySQL 的默认数据库(没有指定 db)
conn=pymysql.connect(
host="localhost",
port=3306,
user="root",
password="123456",
charset="utf8"
)
returnconn
# 创建数据库的函数(如果数据库不存在)
defcreate_database_if_not_exists():
conn=connect_db()
cur=conn.cursor()
# 检查数据库是否存在
cur.execute("SHOW DATABASES LIKE 'attendance_system'")
result=cur.fetchone()
ifresultisNone:
# 数据库不存在,创建数据库
cur.execute("CREATE DATABASE attendance_system")
print("Database 'attendance_system' created successfully.")