PDA

View Full Version : What is the equivalent code lines to interbase jdbc url?



xeoshow
6th January 2017, 05:47
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++;
......
......


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:


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


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.

ChrisW67
6th January 2017, 08:35
Does the program emit a warning at runtime about the encoding?

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.
You get the row without recognisable Chinese characters or the row is completely missing?

Inside the loop add a line :
QString temp = tableModel.record(i).value("ITEM").toString();
Single step the program in the debugger and inspect the value in temp. Is it correct?
Is whatever LOG() writes to Unicode capable?

GBK is an extension of GB2312. Are the broken characters part of the extensions and therefore not understood by gb2312?

xeoshow
6th January 2017, 12:51
Thanks for kind help.



You get the row without recognisable Chinese characters or the row is completely missing?
the row is completely missing when doing select sql execution.



Single step the program in the debugger and inspect the value in temp. Is it correct?
Is whatever LOG() writes to Unicode capable?


If the table only contains rows with Chinese, tableModel.rowCount() will be 0, so the loop is not executed at all.
Regarding LOG(TRACE), I just used easyloggingpp as below, and with no problem.
https://github.com/easylogging/easyloggingpp

Actually, with above code, I can insert rows with Chinese correctly into datase...

xeoshow
7th January 2017, 04:33
My previous jave project, it got 2 parameters in the jdbc url:

charSet=GBK
encoding=NONE

But in the QT, there seems only ISC_DPB_LC_CTYPE parameter for Character set, how about the encoding in QT?