fix:修复数据库链接错误打印至log文件中的乱码问题

This commit is contained in:
duanshengchao 2025-07-18 10:22:29 +08:00
parent c33ba6365f
commit a06fe0f5a1
2 changed files with 3 additions and 1 deletions

View File

@ -108,6 +108,7 @@ bool DatabaseManager::connect(const QString& strConnectionName)
db.setDatabaseName(config.strDBName); db.setDatabaseName(config.strDBName);
db.setUserName(config.strUserName); db.setUserName(config.strUserName);
db.setPassword(config.strPassword); db.setPassword(config.strPassword);
db.setConnectOptions("client_encoding=UTF8");
if(!db.open()) if(!db.open())
{ {
@ -115,7 +116,7 @@ bool DatabaseManager::connect(const QString& strConnectionName)
LOG_ERROR("DB", QString("DB: %1 open failed. connectionName: %2. error: %3") LOG_ERROR("DB", QString("DB: %1 open failed. connectionName: %2. error: %3")
.arg(config.strDBName) .arg(config.strDBName)
.arg(strConnectionName) .arg(strConnectionName)
.arg(QString::fromLocal8Bit(errorText.toLatin1()))); .arg(errorText/*QString::fromUtf8(errorText.toUtf8())*/));
emit errorOccurred(strConnectionName, QString::fromWCharArray(L"数据库打开失败,详情可见日志文件")); emit errorOccurred(strConnectionName, QString::fromWCharArray(L"数据库打开失败,详情可见日志文件"));
db.close(); db.close();
db = QSqlDatabase();//重置为无效数据库对象在下面remove的时候就不会出现"connection '***' is still in use, all queries will cease to work"的警告 db = QSqlDatabase();//重置为无效数据库对象在下面remove的时候就不会出现"connection '***' is still in use, all queries will cease to work"的警告

View File

@ -101,6 +101,7 @@ void Logger::writeToFile(const QString& message)
} }
//打开文件时先键入一个换行符 //打开文件时先键入一个换行符
QTextStream stream(&m_logFile); QTextStream stream(&m_logFile);
stream.setEncoding(QStringConverter::Utf8);//强制UTF-8编码
stream << Qt::endl; stream << Qt::endl;
stream.flush(); //刷新输出缓冲区,确保数据立即写入文件 stream.flush(); //刷新输出缓冲区,确保数据立即写入文件
} }