3

I have a very simple MySQL Table and want to select some of the rows using QSqlQuery.

I am connected to my local development mysql server (Windows; 64-bit).

When I run a SELECT query with other tools like mysql workbench, I always get the correct results (of course!).

Doing the same with QSqlQuery gives me no rows at all!

QString sql = "SELECT `ID`, `Name`, `ModbusID`, `DeviceType` FROM `Devices`;";

qDebug() << sql;

QSqlQuery query(m_db);
if(!query.prepare(sql))
{
    qFatal("could not prepare query");
    return;
}
if(!query.exec())
{
    qFatal("could not execute query");
    return;
}

while(query.next())
    qDebug() << "result";

qDebug() << "finished.";

SQL-Table Definition

CREATE TABLE IF NOT EXISTS `Devices` (
    `ID` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `MeasurementID` INT UNSIGNED NOT NULL,
    `Name` VARCHAR(255) NOT NULL,
    `ModbusID` TINYINT UNSIGNED NOT NULL,
    `DeviceType` INT NOT NULL,
    `TimestampCreated` DATETIME NOT NULL,
    `TimestampLastModified` DATETIME,
    FOREIGN KEY(`MeasurementID`) REFERENCES `Measurements`(`ID`) ON DELETE CASCADE,
    UNIQUE (`MeasurementID`, `Name`)
);

enter image description here

In both programs I am connected to the same local mysql instance. There are no other mysql servers on my machine or on the network. This happens with Oracle's MySQL and MariaDB! So the problem must be in my program.

Am I doing something wrong or what's happening here?

UPDATE After multiple tests the problem only occurs when I select ModbusID. I changed the table definition from

    `ModbusID` TINYINT UNSIGNED NOT NULL,

to

    `ModbusID` INT UNSIGNED NOT NULL,

and now it works. Looks like a bug in QT. But I dont think that this is a good solution. If you know a way to use TINYINT then please write an answer or comment!

feedc0de
  • 3,646
  • 8
  • 30
  • 55

0 Answers0