PDA

View Full Version : QODBC and stored procedures



xgoan
18th November 2008, 12:21
Hi all,

I'm connecting with QODBC to a SQL Server Database; I have a stored procedure that returns a value
How can I get this value?

Thanks

lyuts
18th November 2008, 16:07
This is the simpliest way, I think.



//declare you QSqlQuery
QString qstr = QString("SELECT your_stored_proc(<input params>);");


if ( !query.exec(qstr) ) {
// handle this
}

// if nothing has been returned
if ( !query.first() ) {
// handle this
}

//let us assume that your stored proc returns integer
int var = query.value(0).toInt();

xgoan
18th November 2008, 17:13
With this query:

QString stmt="SELECT [Login](:result, :type, :username, :password);";

I get this error:

[Microsoft][SQL Native Client][SQL Server]Incorrect syntax near '@P1'. [Microsoft][SQL Native Client][SQL Server]Statement(s) could not be prepared. QODBC3: Unable to execute statement"

lyuts
18th November 2008, 17:16
Did you bind your paramter values?
Could you show your code?

xgoan
18th November 2008, 17:27
This is my code :)


ConnectDialog::LoginResult ConnectDialog::login()
{
QSqlQuery q;
QString stmt="SELECT [Login](:result, :type, :username, :password);";
QByteArray md5;

q.setForwardOnly(true);
md5=QCryptographicHash::hash(password().toLatin1() ,
QCryptographicHash::Md5);
q.prepare(stmt);
q.bindValue(":result", QVariant(0), QSql::InOut);
q.bindValue(":type", QVariant(0), QSql::InOut);
q.bindValue(":username", userName());
q.bindValue(":password", md5);
if(!q.exec())
{
qCritical()<<q.lastError().text();
}
else if(!q.first())
{
qCritical()<<q.lastError().text();
}
qDebug()<<q.record();

lyuts
18th November 2008, 17:33
I don't have such an opportunity to compile code, but nevertheless... don't you need to put username and password in quotes(or double-quotes)?
I'm using PostgreSQL and it requires quotes around text/varchar/char params.

xgoan
18th November 2008, 17:43
No, because is a prepared query, QSqlQuery escape it.