SQLite 3的中文读写

调用sqlite3_open函数默认创建的数据库encoding=UTF-8,执行sqlite3_exec时需要将对应的字符串转换为UTF-8格式多字节字符串。比如:

sqlite3* db;
auto retVal = sqlite3_open("test.db", &db);
char* pErrMsg;
auto sql = "create table users(userid varchar(20) PRIMARY KEY, name varchar(50), age int, birthday datetime);";
retVal = sqlite3_exec(db, sql, 0, 0, &pErrMsg);
auto sql2 = _T("insert into users values('administrator', '管理员', 20, '2000-1-1');");
retVal = sqlite3_exec(db, CW2A(sql2, CP_UTF8), 0, 0, &pErrMsg);

返回结果同样需要转换回来:

CppSQLite3DB db;
db.open("test.db");

string sql = CW2A(_T("select * from users where name like '李%';"), CP_UTF8);
auto query = db.execQuery(sql.c_str());

while (!query.eof())
{
    auto name = query.fieldValue("name");
    wstring name2 = CA2W(name, CP_UTF8);

    query.nextRow();
}

 

    原文作者:sqlite
    原文地址: https://www.cnblogs.com/junchu25/p/3425183.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞