PDA

View Full Version : FreeTDS + MSSQL, sqlQuery is active, but no result when use next() func



Pirus
9th January 2013, 10:47
Client program sources on Qt.
Task: Find a "free" way to work with MSSQL under Linux.
I found FreeTDS (QTDS) and unixODBC + FreeTDS (QODBC), chose the option of working through QTDS plugin.

Qt: 4.8.4

Client OS:
Distributor ID: SUSE LINUX
Description: openSUSE 11.1 (i586)

FreeTDS: 0.91
./configure -prefix=/usr/local/freeTDS -enable-msdblib -with-tdsver=8.0
make install

QTDS plugin:
cd %QTDIR%/src/plugins/sqldrivers/tds
qmake -o Makefile "INCLUDEPATH=/usr/local/freeTDS/include" "LIBS=-L/usr/local/freeTDS/lib -lsybdb"
make install

Data MSSQL Server:
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
Copyright (c) Microsoft Corporation Developer Edition (64-bit) on Windows NT 6.0 <X64> (Build 6002: Service Pack 2)

freetds.conf
[Global]
****# TDS protocol version
; Tds version = 4.2

# Whether to write a TDSDUMP file for diagnostic purposes
# (Setting this to /tmp is insecure on a multi-user system)
; Dump file = /tmp/freetds.log
; Debug flags = 0xffff

# Command and connection timeouts
; Timeout = 10
; Connect timeout = 10

# If you get out-of-memory errors, it may mean that your client
# Is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512

[mycar1]
host = mycar1
port = 51451
tds version = 8.0

Test Project Code


#include <QtSql>
#include <QtCore/QCoreApplication>
#include <QTextCodec>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QTDS");
db.setDatabaseName("master");
db.setHostName("MYCAR1");
db.setUserName("admin");
db.setPassword("");
if (!db.open())
{
qDebug() << db.lastError().text();
return 1;
}

QString queryS = "declare @i int set @i=123 select @i";
//QString queryS = "select 123";

qDebug() << queryS;
QSqlQuery query;
query.exec(queryS);
//QTextCodec* codec1251 = QTextCodec::codecForName("CP1251");
if (query.isActive())
{
qDebug() << "Active";
if (query.next())
{
//QString res = codec1251->toUnicode(query.value(0).toByteArray());
//qDebug() << res;
qDebug() << query.value(0).toString();
}
else qDebug() << "No result";
}
else
{
qDebug() << query.lastError().text();
}
return 0;
}



Step by step:
1) I have installed Qt - Opensource, shared libs.
2) Installed the FreeTDS
3) Сompiled QTDS plugin
4) Сonfigured freetds.conf
5) export LD_LIBRARY_PATH = /usr/local/freeTDS/lib
6) Run a test project with a query "select 123", the result is "123" (excellent!)
7) Run a test project with a query "declare @i int set @i = 123 select @i", project returns "No result" (function isActive() returns true, function next() returns false - no data)

I do not understand what's wrong = (

I tried to execute queries through tsql - all excellent.
I tried work through QODBC plugin (unixODBC + freeTDS), problem is the same - a query is active, but no data.
isql like tsql executes queries and returns data correctly.

I think the trouble is somewhere in plugins.

Have any ideas?

Lesiok
9th January 2013, 11:59
whether the query should look like this
declare @i; int set @i = 123; select @i; semicolons.
P.S.
You should test the result of exec(). If it is false dump error message.

Pirus
9th January 2013, 12:51
Now code looks like:



#include <QtSql>
#include <QtCore/QCoreApplication>
#include <QTextCodec>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Prepearing...";
QSqlDatabase db = QSqlDatabase::addDatabase("QTDS");
db.setDatabaseName("master");
db.setHostName("MYCAR1");
db.setUserName("admin");
db.setPassword("");
qDebug() << "Opening...";
if (!db.open())
{
qDebug() << db.lastError().text();
return 1;
}

QString queryS = "declare @i int;\r\n\
set @i=123;\r\n\
select @i;";

qDebug() << queryS;
QSqlQuery query;
qDebug() << "Execute query...";
if (query.exec(queryS))
{
qDebug() << "good exec";
if (query.isActive())
{
qDebug() << "Active";
if (query.next())
{
qDebug() << query.value(0).toString();
}
else qDebug() << "No result";
}
}
qDebug() << query.lastError().text();
return 0;
}


output:
Prepearing...
Opening...
"declare @i int;
set @i=123;
select @i;"
Execute query...
good exec
Active
No result
" "

So, problem persists (((