Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-133826

query.prepare (..) Method When querying the database, when the length of the queried object is more than 12 bits, the query result cannot be obtained, and other things remain unchanged, but the result can be queried after changing the data to 11 bits.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Cannot Reproduce
    • Not Evaluated
    • None
    • 6.8.2
    • SQL Support
    • None
    • Windows

    Description

      Using the first method can not find the result, using the second method can find the result.

      int main(int argc, char* argv[]) {
          QCoreApplication a(argc, argv);
      
          QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
          db.setDatabaseName("testdb");
          db.setHostName("192.168.178.128");
          db.setUserName("testuser");
          db.setPassword("testuser");
          if (!db.open()) {
              qDebug() << db.lastError();
              return 0;
          }
          QSqlQuery query;
          QString createTableQuery = R"(
              CREATE TABLE IF NOT EXISTS account_information123 (
                  account VARCHAR(30) NOT NULL,
                  pass_word VARCHAR(30) NOT NULL,
                  role1_id VARCHAR(30),
                  role1_name VARCHAR(30),
                  role1_lv VARCHAR(30),
                  role1_occupation VARCHAR(30),
                  PRIMARY KEY (account)
              );
          )";
          if (!query.exec(createTableQuery)) {
              qDebug() << query.lastError();
              return 0;
          }
          QString insertQuery = R"(
              INSERT INTO account_information123 (account, pass_word, role1_id, role1_name, role1_lv, role1_occupation)
              VALUES ('12345678901', '12345678901', '12345678901', '雷', '10', '2');
          )";
          if (!query.exec(insertQuery)) {
              qDebug() << query.lastError();
              return 0;
          }
      
          QString mid1[2];
          mid1[0] = "12345678901";
          mid1[1] = "12345678901";
      
          //The first query method
          {
              QSqlQuery q(db);
      
              q.prepare("SELECT role1_id, role1_name, role1_lv, role1_occupation "
                  "FROM account_information123 WHERE account = :account AND pass_word = :pass_word ");
              q.bindValue(":account", mid1[0]);
              q.bindValue(":pass_word", mid1[1]);
      
              if (!q.exec() ||!q.next()) {
                  qDebug() << q.lastError();
                  return 0;
              }
      
              QString message = QString("Role1 ID: %1, Name: %2, Level: %3, Occupation: %4\n")
                  .arg(q.value("role1_id").toString())
                  .arg(q.value("role1_name").toString())
                  .arg(q.value("role1_lv").toString())
                  .arg(q.value("role1_occupation").toString());
              qDebug() << "The result of the query using the first method:" << message;
          }
      
          //The second query method
          {
              QString queryString = QString("SELECT role1_id,role1_name,role1_lv,role1_occupation FROM account_information123 WHERE account=%1 && pass_word =%2")
                  .arg(mid1[0]).arg(mid1[1]);
      
              QSqlQuery q(queryString, db);
              if (!q.exec() || !q.first()) {
                  qDebug() << q.lastError();
                  return 1;
              }
      
              QString message;
              QSqlRecord record = q.record();
              int columnCount = record.count();
              for (int i = 0; i < columnCount; ++i) {
                  message += q.value(i).toString();
                  message += "~";
              }
              qDebug() << "The result of the query using the second method:" << message;
          }
          query.exec("DROP TABLE account_information123");
          return 0;
      }
      

      --> output:
      The result of the query using the first method: "Role1 ID: 12345678901, Name: 雷, Level: 10, Occupation: 2\n"
      The result of the query using the second method: "12345678901~雷~10~2~"

      Attachments

        1. main.cpp
          5 kB
        2. untitled3.pro
          0.6 kB
        3. untitled3.pro.user
          19 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            chehrlic Christian Ehrlicher
            zqf 末学 末学1
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes