Hello,
I am new in the forum, and now met an issue when using the database connection for interbase regarding chracter set and encoding in qt.
Previously I have a java project which also connects to interbase database via below code:
// JDBC driver name and database URL
public static String jdbcDriver = "org.firebirdsql.jdbc.FBDriver";
public static String jdbcUrl = "jdbc:firebirdsql://localhost/c:/FOODBEV.GDB?charSet=GBK&encoding=NONE";
Class.forName(jdbcDriver);
if (conn == null) {
conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPassword);
}
JSONObject updateObject = new JSONObject();
updateObject.put("cmd", "updatecloudmenu");
JSONArray categoryObjectArray = new JSONArray();
updateObject.put("data", categoryObjectArray);
num = 0;
if (stmt == null) {
stmt = conn.createStatement();
}
sql = "SELECT ID, CODE, NAME, CATEGORY, PRICE, PRICE2, PRICE3, UNIT, UNIT2, UNIT3, OPERATION FROM XC_ITEM";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
num++;
......
......
// JDBC driver name and database URL
public static String jdbcDriver = "org.firebirdsql.jdbc.FBDriver";
public static String jdbcUrl = "jdbc:firebirdsql://localhost/c:/FOODBEV.GDB?charSet=GBK&encoding=NONE";
Class.forName(jdbcDriver);
if (conn == null) {
conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPassword);
}
JSONObject updateObject = new JSONObject();
updateObject.put("cmd", "updatecloudmenu");
JSONArray categoryObjectArray = new JSONArray();
updateObject.put("data", categoryObjectArray);
num = 0;
if (stmt == null) {
stmt = conn.createStatement();
}
sql = "SELECT ID, CODE, NAME, CATEGORY, PRICE, PRICE2, PRICE3, UNIT, UNIT2, UNIT3, OPERATION FROM XC_ITEM";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
num++;
......
......
To copy to clipboard, switch view to plain text mode
It can get the rows successfully when having Chinese characters.
And now I want to achieve the same in qt with this same database, my qt code (source code file is UTF-8, and using QTCreator 5.5.1 mingw version) is as below:
db.setHostName("127.0.0.1");
db.setDatabaseName("C:/FOODBEV.GDB");//"C:/TEST.GDB"
db.setUserName("SYSDBA");
db.setPassword("masterkey");
db.setConnectOptions("ISC_DPB_LC_CTYPE=gb2312"); // 对ä¸*文的支æŒ
bool bopen = false;
bopen = db.open();
LOG(TRACE) << "bopen=" << bopen;
tableModel.setTable("XC_ITEM");
tableModel.select();
for (int i = 0; i < tableModel.rowCount(); i++) {
LOG(TRACE) << "tableModel.row=" << i << ", item=" << tableModel.record(i).value("ITEM").toString() << ", des=" << tableModel.record(i).value("DES").toString();
}
......
......
QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE", "ib");
db.setHostName("127.0.0.1");
db.setDatabaseName("C:/FOODBEV.GDB");//"C:/TEST.GDB"
db.setUserName("SYSDBA");
db.setPassword("masterkey");
db.setConnectOptions("ISC_DPB_LC_CTYPE=gb2312"); // 对ä¸*文的支æŒ
bool bopen = false;
bopen = db.open();
LOG(TRACE) << "bopen=" << bopen;
QSqlTableModel tableModel(0, db);
tableModel.setTable("XC_ITEM");
tableModel.select();
for (int i = 0; i < tableModel.rowCount(); i++) {
LOG(TRACE) << "tableModel.row=" << i << ", item=" << tableModel.record(i).value("ITEM").toString() << ", des=" << tableModel.record(i).value("DES").toString();
}
......
......
To copy to clipboard, switch view to plain text mode
When there is no Chinese characters, it works well, but if there is any Chinese characters, then just can not return the rows with Chinese characters.
Anything I did wrong? How to resolve this problem?
Any help is highly appreciated!
Another strange thing is: I found using above code, I can successfully insert into tables with rows having Chinese characters without any garbage characters, but just could not query with the correct result with regard to those rows having Chinese characters.
Bookmarks