【 使用环境 】测试环境
【 OB 】
【 使用版本 】4.3
【问题描述】使用python插入语句,报错Character set ‘45’ is not a compiled character set and is not specified in the ‘C:\Program Files\MySQL\MySQL Server 9.2\share\charsets\Index.xml’ file
我是使用如下代码进行插入操作:
import mysql.connector
from datetime import datetime
import json
from .config import DatabaseConfig
from typing import Any, Dict
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class DatabaseManager:
def __init__(self, config: DatabaseConfig = DatabaseConfig()):
self.config = config
def get_connection(self):
return mysql.connector.connect(
host=self.config.host,
port=self.config.port,
user=self.config.user,
password=self.config.password,
database=self.config.database,
charset=self.config.charset,
use_unicode=True,
connect_timeout=self.config.connect_timeout
)
def save_llm_response(self, response: Any, prompt: str) -> bool:
if response is None:
logger.error("Response is None, skipping database save")
return False
conn = None
cursor = None
try:
conn = self.get_connection()
cursor = conn.cursor()
created_time = datetime.now()
if hasattr(response, 'created') and response.created is not None:
created_time = datetime.fromtimestamp(response.created)
model = getattr(response, 'model', 'unknown')
chat_id = getattr(response, 'id', None)
if not hasattr(response, 'choices') or not response.choices:
logger.error("Response has no choices")
return False
choice = response.choices[0]
content = choice.message.content if hasattr(choice, 'message') else None
finish_reason = choice.finish_reason if hasattr(choice, 'finish_reason') else None
index = choice.index if hasattr(choice, 'index') else 0
role = choice.message.role if hasattr(choice, 'message') else 'assistant'
usage = getattr(response, 'usage', None)
completion_tokens = usage.completion_tokens if usage else 0
prompt_tokens = usage.prompt_tokens if usage else 0
total_tokens = usage.total_tokens if usage else 0
cursor.execute("""
INSERT INTO llm_records (
......
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""", (
......
) if hasattr(response, 'model_dump') else '{}'
))
conn.commit()
return True
except Exception as e:
logger.error(f"Error saving to database: {str(e)}")
if conn:
conn.rollback()
return False
finally:
if cursor:
cursor.close()
if conn:
conn.close()
是不是哪里配置不对,或者没有用对jdbcdriver?我用客户端(dbeaver)倒没有出现这个问题。
谢谢