[Qt-interest] Variants and Binds

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Thu Sep 24 02:45:54 CEST 2009


Jason wrote:

> I wish to add a new kind of class to QVariant, so that I can 
> use it in a SQL bind.
> 
> Specifically, I need to provide access to database 
> expressions. For  instance a QDate object is serialized to 
> to_char('2001-01-01', 'YYYY-MM-DD') when used in Oracle. I 
> need to provide a very similar thing in that I need 
> SEQUENCE.nextval (an expression, not a string) so that my 
> call is bindValue(":rowid", 
> Expression("TABLE_ID_SEQUENCE.nextval")); If I use a 
> QByteArray, I get "TABLE_ID_SEQUENCE.nextval", the string, 
> and that isn't a valid numeric record ID. 
> 
> So I need to know how I can create  a datatype that won't be quoted. 
> 
> Thanks!

Hi Jason, 

Even if you did add the extra type to QVariant, it would not work.  A SQL
expression can only be evaluated on the server, and you can only bind values
on the client.  

I think you can either: 

- Put the expression into the actual SQL statement, for example, insert into
tableName(fieldName1, fieldName2...) values (TABLE_ID_SEQUENCE.nextval,
:fieldName2...)

	Then just bind the other fields. 

- Or you need to explicitly select the value from the sequence first: 

	myQuery.exec("select TABLE_ID_SEQUENCE.nextval from dual");
	myQuery.next();
	int sequence = myQuery.value(0).toInt;

then bind that value just like the fields.  

Hope that helps, 

Tony.





More information about the Qt-interest-old mailing list