A recent project used QT to develop a client in vs2008. Due to the small amount of data on the client, the database uses sqlite3, which is small in size, fast, easy to maintain and out-of-the-box. It is good!
During development, the sqlite3.exe client is used to operate SQLite database files. There is no problem with insert and select, and Chinese support is well supported without garbled characters; however
The data inserted in the sqlite3 console is garbled when it is queried by QT.!
However, all the records inserted by QT to SQLite data files are normal without garbled characters.
The original cause is that the encoding format of sqlite3.exe Console (on my computer) is GBK, the content inserted into the database through the console is GBK format, but the database uses the UTF-8 format by default, therefore, the database stores garbled characters, while the strings in QT use Unicode encoding. Therefore, the strings found by QT are garbled characters ~
But why is it normal to query it on the console? The reason is still in the console: the console adds GBK format records to the database, these records are stored in the database UTF-8; when using the console to retrieve data outside, the retrieved data is in GBK format, and then the console uses GBK decoding. So everything works, just like nothing has happened ~
Similarly, in QT, Unicode encoding is used. Therefore, all records added to the database are in unicode format. If you use QT to retrieve data, it is also a normal text ~
However, the data is garbled in the console!
What should I do? If you are reluctant to use the console like meSqlitespy ToolYou can use tools other than sqlite3.exe to operate SQLite database files, including memory databases.
The text is complete.
The following is a bit of experience, hoping to help students with the "garbled characters in QT" problem:
1. qtextcodec
Qtextcodec: setcodecforlocale (qtextcodec: codecforname ("gb2312"); // sets the locale encoding format. This locale encoding is mainly provided for subsequent code to use qtextcodec: codecforlocale () qtextcodec: setcodecfortr (qtextcodec: codecforlocale (); // use qobject: TR ("... ") to convert a String constant, it is used to explain to QT that the string to be converted uses the xxx encoding format qtextcodec: setcodecforcstrings (qtextcodec: codecforlocale ()); // similar to the previous sentence, it indicates that the input string constant uses the xxx encoding format when constructing a qstring using a String constant.
2. The sqliteformat is used. The following command is executed on the sqlite3.exe console:
Pragma encoding; -- display SQLite current encoding scheme Pragma encoding = "UTF-8"; -- set SQLite encoding scheme to UTF-8PRAGMA encoding = "UTF-16 "; -- set SQLite encoding scheme to UTF-16PRAGMA encoding = "UTF-16le"; Pragma encoding = "UTF-16be ";
References:
1. QT text encoding conversion method
2. How can I change SQLite encoding iso8859 to utf8?