How to insert null value

Forum for posting problems using QxOrm library

How to insert null value

Postby wgargoyle » Thu Jun 16, 2011 4:30 am

HI.
How to insert record with null column values.
Thanks!!!
wgargoyle
 

Re: How to insert null value

Postby QxOrm admin » Thu Jun 16, 2011 8:15 am

Hi,

Good question :)

QxOrm is based on QtSql module of Qt library and it uses the class QSqlQuery to communicate with database :
http://doc.qt.nokia.com/latest/qsqlquery.html

In QSqlQuery class documentation, there is :
To bind a NULL value, use a null QVariant; for example, use QVariant(QVariant::String) if you are binding a string.


So to bind a NULL value with QxOrm library, you need to map a QVariant property in the function : void register_class(QxClass<T> & t).
For example :
Code: Select all
namespace qx {
template <> void register_class(QxClass<my_class> & t)
{
   //...
   t.data(& my_class::m_variant, "variant"); // m_variant is a QVariant type
   //...
}}


Then, when you insert/update your object into database, if this property is a NULL QVariant, normally it would insert a NULL value into database.

BUT : I think you have to patch 2 files of QxOrm library. I will add this patch in the next version of QxOrm library (released current july).

* in the file "./inl/QxCommon/QxStringCvt_ToVariant.inl", add this code :
Code: Select all
template <> struct QxStringCvt_ToVariant< QVariant > {
static inline QVariant toVariant(const QVariant & t, const QString & format, int index)
{ Q_UNUSED(t); Q_UNUSED(format); Q_UNUSED(index); return t; } };


* in the file "./inl/QxCommon/QxStringCvt_FromVariant.inl", add this code :
Code: Select all
template <> struct QxStringCvt_FromVariant< QVariant > {
static inline qx_bool fromVariant(const QVariant & v, QVariant & t, const QString & format, int index)
{ Q_UNUSED(format); Q_UNUSED(index); t = v; return qx_bool(true); } };
QxOrm admin
 

Re: How to insert null value

Postby wgargoyle » Thu Jun 16, 2011 11:04 am

Thanks a lot!
wgargoyle
 

Re: How to insert null value

Postby QxOrm admin » Sun Jun 26, 2011 7:16 pm

QxOrm 1.1.7 just released : you can now insert NULL values into database using QVariant type.
QxOrm admin
 


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron