Qt creator tableView not displaying data from my sql server
-
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); }
-
What's the relationship to Qt here when you get errors in winapi calls?
Use the QSql module. -
@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.
-
@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.