PDA

View Full Version : Stored procedure return values problem



jgreetham
29th August 2007, 18:13
Hi all,

I'm having a problem with a character value being returned by one of my stored procedures in Oracle using the OCi driver.

I create a QString to retrieve the data into but I found that I also have to create a char* variable to bind to the procedure value:

QString fp;
char fpc[525];
...
query.bindValue(1, fpc, QSql::out);
...
query.exec();
...
fp = query.boundValue(1).toString();

Is this the correct approach? The thing is, the value of fp is, of course, fixed length & padded with garbage characters after the "real" value. How can I get rid of those garbage characters? Maybe this isn't really a stored procedure question just a character string manipulation question.

Thanks very much,
Jim

marcel
29th August 2007, 18:29
Why do you use a char* for?
The second parameter for the QSqlResult::bindValue is a QVariant, meaning that you can pass a QString or even an empty QVariant.
You can retrieve the QString with QVariant::toString().

You're getting rubbish at the end of the char array because it is not null terminated.

Regards

jgreetham
29th August 2007, 19:46
Thanks, Marcel.

The reason I'm using char* for the bind variable is that when I use the QString, I get an error about the character buffer being too small.

Is there, maybe, something I need to change on the stored procedure? The particular parameter is currently specified as VARCHAR2 with no field width value - this returned character string could be thousands of characters long (but it's not a clob).

Cheers,
Jim

marcel
29th August 2007, 20:00
What if you pass an empty QVariant?


QVariant v;
query.bindValue(1, v, QSql:: Out);
QString s = v.toString();


This way, the entire buffer should fit in there, even if the length is unknown.

BTW, I thought VARCHAR2 always has a size entry.

Regards

jgreetham
29th August 2007, 20:43
Hi Marcel,

I tried the QVariant but got a null returned. The only way I have been able to get a returned value is to use a char*.

The actual problem I have in using the returned value, which is a list of numbers, is that after converting the comma-delimited string to a QStringList, when converting each number string to a double, the last number gets an error because of, I guess, the non-null-terminated value.

(The numbers are vertices of a polygon retrieved from an Oracle Spatial object)

Cheers,
Jim

marcel
29th August 2007, 20:48
then use this prior to calling bindValue:


memset(fpc, 0, sizeof(char)*525);


This way you make sure you will always have a null at the end of your string, in case the stored procedure returns a smaller number of characters.

You can even increase the size of the array to 1024 or 2048, but make sure to create it on the heap, not on the stack.

Regards

jgreetham
29th August 2007, 21:14
Rats! That gives a null returned value.

jgreetham
10th September 2007, 17:38
Hi all,

My company finally got my Qt maintenance renewed so I could pose this to Trolltech. They say it is a bug in Qt 3.3.x which also exists in Qt 4.

I have had to revise my stored procedures to append a ",end" at the end of any returned strings so I can then extract the substring to the left of that.:(

Cheers,
Jim