74 lines
1.9 KiB
Python
74 lines
1.9 KiB
Python
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}")
|
||
connection.commit()
|
||
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) |