qt4內(nèi)置有sqlite插件,可以直接使用sqlite.但是發(fā)現(xiàn)插入中文時(shí)會有亂碼問題。
以windows為例,qt4內(nèi)置編碼為system(GBK).而sqlite內(nèi)部編碼為unicode.
如果插入中文首先要轉(zhuǎn)換為unicode.而從數(shù)據(jù)庫讀取時(shí)則不需要,因?yàn)閝t會自動偵測編碼,
實(shí)例代碼:
//插入記錄
QSqlQuery query;
QByteArray sql = "insert into person values(1, 'hello', '你好!')";
QTextCodec *codec = QTextCodec::codecForName("GBK");
QString string = codec->toUnicode(sql);
query.exec(string);
//讀入記錄
QSqlQuery query("select * from person");
while (query.next()) {
QString string = query.value(2).toString();;
QMessageBox::information(0, "infa", string, QMessageBox::Ok);
}