当前位置: 首页 > ai >正文

SQLite 中文写入失败问题总结

SQLite 中文写入失败问题总结与解决方案

在 Windows 下使用 C++ 操作 SQLite 数据库时,中文字段经常出现 写入成功但内容显示为 BLOB乱码 的问题。根本原因在于 SQLite 要求字符串以 UTF-8 编码 存储,而默认的 std::string 中文通常是 GB2312/ANSI 编码。

🧩 问题现象

在 C++ (如 Visual Studio) 中使用 std::string 把含有中文的字符串插入到 SQLite 数据库时:

  • 插入后数据表中文本列显示为 BLOB
  • 或者表面显示为 乱码,无法正常显示中文

示例

std::string desc = "搬运动作:从 Port2 取片 → Port3 放片";
m_pDB->executeQuery("INSERT INTO ... VALUES ('" + desc + "');");

结果: description 列在 SQLite 中显示为 BLOB,而不是文本

在这里插入图片描述


🔍 根本原因

SQLite 要求所有 TEXT 类型的字符串必须为 UTF-8 编码。

而 VS 编译器中直接写入 std::string 的中文,实际编码是 GB2312 / GBK / ANSI,并非 UTF-8

导致结果:

  • SQLite 无法识别非 UTF-8 内容,转而存储成 BLOB

✅ 解决方案

✅ 方法一:使用 ansiToUtf8() 将 ANSI 转换为 UTF-8

// 本地编码转为 UTF-8
std::string TransferManager::ansiToUtf8(const std::string& ansiStr) {int wideLen = MultiByteToWideChar(CP_ACP, 0, ansiStr.c_str(), -1, nullptr, 0);std::wstring wideStr(wideLen, 0);MultiByteToWideChar(CP_ACP, 0, ansiStr.c_str(), -1, &wideStr[0], wideLen);int utf8Len = WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, nullptr, 0, nullptr, nullptr);std::string utf8Str(utf8Len, 0);WideCharToMultiByte(CP_UTF8, 0, wideStr.c_str(), -1, &utf8Str[0], utf8Len, nullptr, nullptr);utf8Str.pop_back(); // 去掉最后的 '\0'return utf8Str;
}

使用:

std::string raw = "搬运动作:从 Port2 取片 → Port3 放片"; // 系统本地编码
std::string utf8 = TransferManager::ansiToUtf8(raw);
data.strDescription = utf8;

在这里插入图片描述


✅ 方法二:如果使用了 Unicode 工程(如 std::wstring / CStringW

可使用:

std::string TransferManager::wstringToUtf8(const std::wstring& wstr);

或者使用 CStringWstd::wstringUTF-8

CStringW wdesc = L"搬运动作:从 Port2 取片 → Port3 放片";
data.strDescription = TransferManager::wstringToUtf8(std::wstring(wdesc));

📦 可选补充:从 SQLite 中读取 UTF-8 → 转为本地编码显示

// UTF-8 转为本地编码
std::string TransferManager::utf8ToAnsi(const std::string& utf8Str) {int wideLen = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, NULL, 0);std::wstring wideStr(wideLen, 0);MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, &wideStr[0], wideLen);int ansiLen = WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, NULL, 0, NULL, NULL);std::string ansiStr(ansiLen, 0);WideCharToMultiByte(CP_ACP, 0, wideStr.c_str(), -1, &ansiStr[0], ansiLen, NULL, NULL);ansiStr.pop_back(); // 去掉最后的 '\0'return ansiStr;
}

可用于日志输出或 UI 控件显示。


🧪 实际检测建议

插入前输出:

printf("准备插入 UTF-8 字符串:%s\n", data.strDescription.c_str());

验证方式:

  • SQLite 中字段类型应为 TEXT,不是 BLOB
  • DB Browser 应正常显示中文,不是问号或乱码

🧠 最佳实践

场景建议方式
插入中文到 SQLiteansiToUtf8
从 SQLite 读取后展示utf8ToAnsi
项目编码设置设置为 UTF-8 无 BOM
VS 源文件保存编码另存为 UTF-8 编码

✅ 总结

只要 std::string 中的中文不是 UTF-8 编码,SQLite 插入后就会出现异常(显示为 BLOB 或乱码)。

http://www.xdnf.cn/news/9751.html

相关文章:

  • 无人设备遥控器之红外技术篇
  • Arbitrum Stylus 合约实战 :Rust 实现 ERC20
  • lua的注意事项2
  • 跟Gemini学做PPT-模板样式的下载
  • PHP序列化和反序列化
  • 包会!在Linux上用bcc运行第一个eBPF程序
  • 【25-cv-05935】Keith律所代理(绿色巴士图)版权维权案
  • STM32 启动文件详解:理解单片机启动的“引导者”
  • 【ARM AMBA APB 入门 1.1 -- APB 读写寄存器 RTL 实现】
  • 443端口:HTTPS通信的安全基石
  • 奥威BI+AI——高效智能数据分析工具,引领数据分析新时代
  • 零基础认知企业级数据分析平台如何落实数据建模(GAI)
  • React---day4
  • LeetCode 395.至少有K个重复字符的最长子串
  • 基于大模型的重度膝关节骨关节炎全流程预测与治疗方案研究
  • c++ opencv 形态学操作腐蚀和膨胀
  • 三套知识系统实践对比:谁真正融入了研发流程?
  • 经典SQL查询问题的练习第一天
  • Spring Tool Suite(STS)4国内下载与安装教程
  • 最优控制:从变分法到庞特里亚金原理
  • Rocky Linux上安装Go
  • 数据可视化--使用matplotlib绘制高级图表
  • 理解频域滤波
  • 构建一个“论文检索 + 推理”知识库服务,支持用户上传 PDF/LATEX 源码后,秒级检索并获得基于内容的问答、摘要、引用等功能
  • ChemDraw 2023|Win英文|化学结构编辑器|安装教程
  • Kotlin 中集合遍历有哪几种方式?
  • Xshell连接Linux时出现Warning:The remote SSH server rejected X11 forwarding request.
  • Linux---系统守护systemd(System Daemon)
  • 江西某石灰石矿边坡自动化监测
  • 【Linux 基础知识系列】第二篇-Linux 发行版概述