QT Mac OS X - ODBC Problem: next() will not push back any records - SQLFetchScroll with return code -1
-
Dear all,
I try the QT SQL functions with an ODBC MySQL Connection. The program gets back the columns but no data with next().
If I use next() then I see on the trace file a "SQLFetchScroll with return code -1 ". If I triy a second option (#ifdef A is true) with boundValues() I see not SQL_ERROR but I also get no rows back. I tried different ODBC drivers. Everything is working with isql and I see the right amount of fields in the row and can read the name of the fields.Would be great if someone would have a hint for me what could cause the problem.
Here is the code of my little test program:
@
#include "odbcmainwindow.h"
#include "ui_odbcmainwindow.h"#include <QSqlDriver>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>#define DB_DRIVER "QODBC3"
// you need to replace the following macros with the values relevant to
// your DSN.
// DB_DBNAME = the name of the DSN you have created in the odbc.ini file
#define DB_DBNAME "rufbereitschaft"
// DB_USER = a username valid for your database
#define DB_USER "root"
// DB_PASSWD = password for DB_USER
#define DB_PASSWD ""
// DB_HOST is not relevant to DSNs
#define DB_HOST "127.0.0.1"
// QT will create an ODBC connection string of
// DSN=DB_DBNAME;UID=DB_USER;PWD=DB_PASSWDODBCMainWindow::ODBCMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ODBCMainWindow)
{
ui->setupUi(this);
db_run();
}ODBCMainWindow::~ODBCMainWindow()
{
delete ui;
}void ODBCMainWindow::db_run()
{
int qsize=0;
int record_count=0;
QStringList all_result;QStringList drivers = QSqlDatabase::drivers();
ui->listWidget_drivers->addItems(drivers);QSqlDatabase mydb = QSqlDatabase::addDatabase( DB_DRIVER );
mydb.setDatabaseName( DB_DBNAME );
mydb.setUserName( DB_USER );
mydb.setPassword( DB_PASSWD );
mydb.setHostName( DB_HOST );
mydb.setConnectOptions("SQL_ATTR_TRACE=SQL_OPT_TRACE_ON");
mydb.open();
if ( mydb.isOpen() )
{
QSqlQuery myquery;myquery.setForwardOnly(true); // why this ? see source: http://www.archivum.info/qt-interest@trolltech.com/2007-09/00036/Re-Problem-with-ODBC-and-QSqlquery-in-linux.html
// "select * from mysql.help_topic;"
myquery = mydb.exec(QString("select * from lsy_storage;"));if( myquery.isActive() )
{
QSqlRecord rec = myquery.record();int num_of_col = rec.count();
ui->listWidget_messages->addItem(QString("Result returned number of Columns: %1").arg(num_of_col));// QSqlDriver::QuerySize
QSqlDriver *sql_driver = mydb.driver();
ui->listWidget_messages->addItem(QString("Okay: using driver: %1").arg(mydb.driverName()));if( sql_driver->hasFeature(QSqlDriver::QuerySize) )
{
qsize=myquery.size();
ui->listWidget_messages->addItem(QString("Okay: View of the Result: %1 %2").arg(mydb.driverName()).arg(qsize));
}
else
{
ui->listWidget_messages->addItem(QString("WARNING: Driver is not supporting QuerySize !"));
qsize=-1;
}
// wichtig auch: QSqlDriver::BLOB
if( ! sql_driver->hasFeature(QSqlDriver::BLOB) )
ui->listWidget_messages->addItem(QString("WANING Driver is not supporting QSqlDriver::BLOB"));
// QSqlDriver::EventNotifications
if( ! sql_driver->hasFeature(QSqlDriver::EventNotifications ) )
ui->listWidget_messages->addItem(QString("WARNING: Driver is not supporting QSqlDriver::EventNotifications"));// Bezeichner der Tabelle auslesen (Tabel Header):
for(int col=0; col < num_of_col; col++)
{
ui->listWidget_output->addItem(QString("get colume number %1 from query %2").arg(col).arg(rec.fieldName(col)));
}#define B
#ifdef A
if( myquery.isSelect() )
{
QList<QVariant> list = myquery.boundValues().values();
ui->listWidget_output->addItem(QString("DEFINED A: QList list size is %1").arg(list.size()));
for (int i = 0; i < list.size(); ++i)
{
record_count++;
all_result.append(list.at(i).toString().toAscii().data());
}
}
else
ui->listWidget_messages->addItem(QString("ERROR: isSelect() returned false !"));
#endif#ifdef B
ui->listWidget_output->addItem(QString("DEFINED B: myquery.next()"));
if( myquery.isSelect() )
{
while( myquery.next() )
{
record_count++;
for(int col=0; col < num_of_col; col++)
{
all_result.append(myquery.value(col).toString());
}
}
}
else
ui->listWidget_messages->addItem(QString("ERROR: isSelect() returned false !"));
#endifui->listWidget_output->addItems(all_result);
ui->listWidget_output->addItem(QString("processed %1 records").arg(record_count));
}
else
{
if( mydb.lastError().number() == -1 )
ui->listWidget_messages->addItem(QString("ERROR: query not isActive State ! last error set ! -1 can not determinate error !!!"));
else
ui->listWidget_messages->addItem(QString("ERROR: query not isActive State last error set ! Number: %1").arg(mydb.lastError().databaseText()));
}
mydb.close();
}
else
{
ui->listWidget_messages->addItem(QString("ERROR: DB nicht offen ! Fehler: %1").arg(mydb.lastError().driverText()));
}
} // END ODBCMainWindow::db_run()
@I tried mysql-connector-odbc-5.1.8-osx10.6-x86-64bit libmyodbc5-5.1.8.so
and also actualtechnologies ODBC driver on Mac OS X Lion with QT Version 4.7.4 (I know....this version of Mac OS X isn't supported yet...) -
I have the same problem; If you do not setForwardOnly to true then it hang indefinitely. If you set it to true then calling next() will return no data. I have written code directly accessing ODBC APIs and using different ODBC drivers will work correctly. It's not the ODBC driver or ODBC APIs problem.
I had the same problem on Snow Leopard as well!
There were some articles on the net about this issue being fixed in 4.8. I have tried 4.8 and has the same issue!
-
Hello EddyHahn !
Thank's a lot to share this info ! Okay...then I have to go the same way to build my own routines to use ODBC.
-
I tried the last mysql connector 5.1.11 and I experienced the same problem ! Could you explain how directly accessing ODBC API ?
Thanks
-
For anyone interested, I've posted about how to implement a workaround. It requires that you download the Qt sources and rebuild the ODBC plugin from source. You can see the details here: http://qt-project.org/forums/viewthread/18583/
-
Hello,
I'm trying to deploy the FreeTDS driver on a user’s mac, in the development machine it works fine, I have the drivers in the resource folder and the application picks it up from there and it works on the development machine. But in the user’s mac which has no knowledge about the FreeTDS or UnixODBC, fails to load the driver. I know that I have to point the application to look for the driver, not sure how to do it? The error is I receive on the user’s machine is:
@Error: QSqlError(0, "QODBC3: Unable to connect", "[unixODBC][Driver Manager]Data source name not found, and no default driver specified")@
And my .pro file has library link to odbc, that is -lodbc, but I cant seem to link freeTDS since they are static libs.
Now does this require the users to install freeTDS and unixODBC, as in the developer machine??
Any help will be appreciated, thanks