baoxiang/check_chests.py

54 lines
2.0 KiB
Python
Raw Normal View History

2025-12-16 18:06:50 +08:00
#!/usr/bin/env python3
"""
检查MySQL数据库中的宝箱数据
"""
import pymysql
from datetime import datetime
def check_chests():
"""检查宝箱数据"""
print("=== 检查MySQL数据库中的宝箱数据 ===")
try:
# 连接到MySQL数据库
connection = pymysql.connect(
host='localhost',
user='root',
password='taiyi1224',
database='baoxiang',
charset='utf8mb4'
)
with connection.cursor() as cursor:
# 查询所有宝箱
cursor.execute("SELECT id, title, created_at, countdown_seconds, status FROM chests")
chests = cursor.fetchall()
print("宝箱数据:")
print("ID | Title | Created At | Countdown Seconds | Status")
print("---|-------|------------|------------------|-------")
for chest in chests:
chest_id, title, created_at, countdown_seconds, status = chest
print(f"{chest_id} | {title} | {created_at} | {countdown_seconds} | {status}")
# 解析创建时间
try:
# MySQL datetime格式 (YYYY-MM-DD HH:MM:SS)
created_time = datetime.strptime(str(created_at), '%Y-%m-%d %H:%M:%S')
now = datetime.utcnow() # 使用UTC时间进行计算
elapsed = (now - created_time).total_seconds()
time_remaining = max(0, int(countdown_seconds - elapsed))
print(f" -> Created: {created_time}")
print(f" -> Now (UTC): {now}")
print(f" -> Elapsed: {elapsed:.2f}s, Time remaining: {time_remaining}s")
except Exception as e:
print(f" -> Error calculating time: {e}")
connection.close()
except Exception as e:
print(f"MySQL数据库连接错误: {e}")
if __name__ == "__main__":
check_chests()