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();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM