Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Qt creator tableView not displaying data from my sql server

Qt creator tableView not displaying data from my sql server

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
4 Posts 3 Posters 542 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Faris Elbaz
    wrote on last edited by
    #1

    In the below code, when i click the LoabButton, it does not display anything to my tableView, i debugged it slowly and found that an error called "Failed to allocate function handle" after cleaning and rebuilding the project, it no longer displays that message but nothing displays to the tableView. I know that all my other code works by using debug statements. Any Help would be appreciated.

    void mainstuff::on_LoadButton_clicked() {
        // Check if a table name is selected
        if (selectedTableName.isEmpty()) {
            QMessageBox::warning(this, "Warning", "Please select a table from the list first.");
            return;
        }
        qDebug() << selectedTableName;
    
        // Now load the table using the selected table name
        SQLCHAR query[512];
        snprintf((char*)query, sizeof(query), "SELECT * FROM %s", selectedTableName.toUtf8().constData());
        qDebug() << "Query:" << QString::fromUtf8(reinterpret_cast<const char*>(query));
    
        SQLHSTMT hstmt;
        SQLRETURN ret = SQLAllocHandle(SQL_HANDLE_ENV, hdbc, &hstmt);
        if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
            QMessageBox::critical(this, "Error", "Failed to allocate statement handle");
            return;
        }
    
        ret = SQLExecDirect(hstmt, query, SQL_NTS);
        if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
            SQLSMALLINT rec = 0;
            SQLCHAR state[7];
            SQLINTEGER native;
            SQLCHAR msg[1024];
            while (SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, ++rec, state, &native, msg, sizeof(msg), NULL) == SQL_SUCCESS) {
                QMessageBox::critical(this, "Error", QString::fromLocal8Bit(reinterpret_cast<char*>(state)) + ": " + QString::fromLocal8Bit(reinterpret_cast<char*>(msg)));
            }
            SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
            return;
        }
    
        ui->tableView->setModel(nullptr);
    
        SQLSMALLINT columnCount;
        ret = SQLNumResultCols(hstmt, &columnCount);
        if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
            QMessageBox::critical(this, "Error", "Failed to get number of columns");
            SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
            return;
        }
    
        // Retrieve the column names
        QStringList columnNames;
        for (int i = 0; i < columnCount; i++) {
            SQLCHAR columnName[256];
            SQLSMALLINT nameLength;
            ret = SQLDescribeCol(hstmt, i + 1, columnName, sizeof(columnName), &nameLength, NULL, NULL, NULL, NULL);
            if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
                QMessageBox::critical(this, "Error", "Failed to get column name");
                SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
                return;
            }
            columnNames << QString::fromLocal8Bit(reinterpret_cast<char*>(columnName)).left(nameLength);
        }
    
        // Create a new model to hold the data
        QStandardItemModel *tableModel = new QStandardItemModel(this);
    
        // Set the column headers
        tableModel->setHorizontalHeaderLabels(columnNames);
    
        // Get the data rows
        while (SQLFetch(hstmt) == SQL_SUCCESS) {
            QList<QStandardItem*> rowItems;
            for (int i = 0; i < columnCount; i++) {
                SQLCHAR data[256];
                SQLLEN dataLength;
                ret = SQLGetData(hstmt, i + 1, SQL_C_CHAR, data, sizeof(data), &dataLength);
                if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
                    QMessageBox::critical(this, "Error", "Failed to get row data");
                    SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
                    return;
                }
                QStandardItem* item = new QStandardItem(QString::fromLocal8Bit(reinterpret_cast<char*>(data)));
                rowItems << item;
            }
            tableModel->appendRow(rowItems);
        }
    
        // Set the model for the tableView
        ui->tableView->setModel(tableModel);
    
        // Clean up
        SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
    }
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • F Faris Elbaz

      In the below code, when i click the LoabButton, it does not display anything to my tableView, i debugged it slowly and found that an error called "Failed to allocate function handle" after cleaning and rebuilding the project, it no longer displays that message but nothing displays to the tableView. I know that all my other code works by using debug statements. Any Help would be appreciated.

      void mainstuff::on_LoadButton_clicked() {
          // Check if a table name is selected
          if (selectedTableName.isEmpty()) {
              QMessageBox::warning(this, "Warning", "Please select a table from the list first.");
              return;
          }
          qDebug() << selectedTableName;
      
          // Now load the table using the selected table name
          SQLCHAR query[512];
          snprintf((char*)query, sizeof(query), "SELECT * FROM %s", selectedTableName.toUtf8().constData());
          qDebug() << "Query:" << QString::fromUtf8(reinterpret_cast<const char*>(query));
      
          SQLHSTMT hstmt;
          SQLRETURN ret = SQLAllocHandle(SQL_HANDLE_ENV, hdbc, &hstmt);
          if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
              QMessageBox::critical(this, "Error", "Failed to allocate statement handle");
              return;
          }
      
          ret = SQLExecDirect(hstmt, query, SQL_NTS);
          if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
              SQLSMALLINT rec = 0;
              SQLCHAR state[7];
              SQLINTEGER native;
              SQLCHAR msg[1024];
              while (SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, ++rec, state, &native, msg, sizeof(msg), NULL) == SQL_SUCCESS) {
                  QMessageBox::critical(this, "Error", QString::fromLocal8Bit(reinterpret_cast<char*>(state)) + ": " + QString::fromLocal8Bit(reinterpret_cast<char*>(msg)));
              }
              SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
              return;
          }
      
          ui->tableView->setModel(nullptr);
      
          SQLSMALLINT columnCount;
          ret = SQLNumResultCols(hstmt, &columnCount);
          if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
              QMessageBox::critical(this, "Error", "Failed to get number of columns");
              SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
              return;
          }
      
          // Retrieve the column names
          QStringList columnNames;
          for (int i = 0; i < columnCount; i++) {
              SQLCHAR columnName[256];
              SQLSMALLINT nameLength;
              ret = SQLDescribeCol(hstmt, i + 1, columnName, sizeof(columnName), &nameLength, NULL, NULL, NULL, NULL);
              if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
                  QMessageBox::critical(this, "Error", "Failed to get column name");
                  SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
                  return;
              }
              columnNames << QString::fromLocal8Bit(reinterpret_cast<char*>(columnName)).left(nameLength);
          }
      
          // Create a new model to hold the data
          QStandardItemModel *tableModel = new QStandardItemModel(this);
      
          // Set the column headers
          tableModel->setHorizontalHeaderLabels(columnNames);
      
          // Get the data rows
          while (SQLFetch(hstmt) == SQL_SUCCESS) {
              QList<QStandardItem*> rowItems;
              for (int i = 0; i < columnCount; i++) {
                  SQLCHAR data[256];
                  SQLLEN dataLength;
                  ret = SQLGetData(hstmt, i + 1, SQL_C_CHAR, data, sizeof(data), &dataLength);
                  if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) {
                      QMessageBox::critical(this, "Error", "Failed to get row data");
                      SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
                      return;
                  }
                  QStandardItem* item = new QStandardItem(QString::fromLocal8Bit(reinterpret_cast<char*>(data)));
                  rowItems << item;
              }
              tableModel->appendRow(rowItems);
          }
      
          // Set the model for the tableView
          ui->tableView->setModel(tableModel);
      
          // Clean up
          SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
      }
      
      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      What's the relationship to Qt here when you get errors in winapi calls?
      Use the QSql module.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      F 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        What's the relationship to Qt here when you get errors in winapi calls?
        Use the QSql module.

        F Offline
        F Offline
        Faris Elbaz
        wrote on last edited by
        #3

        @Christian-Ehrlicher I cant use Qsql for some reason it doesnt work for me on mac when my sql server is running through a container on docker. I'n not exactly sure if the error is caused by QT or something wrong in my statement, so i was checking to see if its a known problem that occurs when trying to use odbc.

        SGaistS 1 Reply Last reply
        0
        • F Faris Elbaz

          @Christian-Ehrlicher I cant use Qsql for some reason it doesnt work for me on mac when my sql server is running through a container on docker. I'n not exactly sure if the error is caused by QT or something wrong in my statement, so i was checking to see if its a known problem that occurs when trying to use odbc.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Faris-Elbaz hi,

          Do you mean a MySQL server ? If so, did you build the MySQL plugin ?
          Otherwise, PostgreSQL can also be a solution as the backend is already built.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved