调用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,&pErrMsg); auto sql2 = _T("insert into users values('administrator','管理员',20,'2000-1-1');"); retVal = sqlite3_exec(db,CW2A(sql2,CP_UTF8),&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(); }