Files
EmojiTextGenerator/models/emoji_db.py
2025-12-07 14:31:28 +08:00

73 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from models.connect_db import connect_db
from models.logger import setup_logger
logger = setup_logger()
connection = connect_db()
def update_description(emoji_uuid: str, description: str) -> bool:
"""
更新表情包描述词
Args:
emoji_uuid: 表情包UUID
description: 新的描述词
Returns:
成功返回True失败返回False
"""
try:
sql = """
UPDATE emoji
SET description = %s
WHERE uuid = %s
"""
params = (description, emoji_uuid)
with connection.cursor() as cursor:
affected_rows = cursor.execute(sql, params)
if affected_rows > 0:
logger.info(f"成功更新表情包描述UUID: {emoji_uuid}")
return True
else:
logger.warning(f"未找到要更新的表情包UUID: {emoji_uuid}")
return False
except Exception as e:
logger.error(f"更新表情包描述失败: {e}")
return False
def check_emoji(uuid: str) -> bool:
"""
检查UUID是否存在于emoji表中
Args:
uuid: 要检查的UUID字符串
Returns:
uuid 在数据库中存在则为 True否则 False
"""
# 参数验证
if not uuid or not isinstance(uuid, str):
logger.warning(f"无效的UUID参数: {uuid}")
return False
try:
with connection.cursor() as cursor:
# 执行查询
sql = "SELECT 1 FROM emoji WHERE uuid = %s LIMIT 1"
cursor.execute(sql, (uuid,))
# 如果查询到结果则返回True
result = cursor.fetchone()
return result is not None
except Exception as e:
logger.error(f"检查UUID失败: {uuid}, 错误: {e}")
return False
if __name__ == "__main__":
record = check_emoji("ffe1663c-44e1-4719-a5ba-01485f70a87e")
print(record)