tags:

views:

53

answers:

1

Hello.

If i create a table using Qt and SQLite that has a nullable columns (columns without "NOT NULL") and add a null value into it (using NULL QVariant) retrieving such value back will return not and NULL QVariant, but QVariant of type String and value "". Am i doing something wrong or Qt with SQLite can't distinguish between NULL value in database and default value (0, "", default date ect)? My test code:

#include <QtCore/QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QVariant>
#include <QSqlRecord>

int main(int argc, char *argv[])
{
  QCoreApplication a(argc, argv);
  QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
  db.setHostName( "MyHost" );
  db.setDatabaseName( "c:\\tmp\\db.sql" );
  db.open();

  QSqlQuery( "CREATE TABLE a ( b INT )", db );
  QSqlQuery query( db );
  query.prepare( "INSERT INTO a ( b ) VALUES ( :b )" );
  //  Insert NULL value into table (NULL variant).
  query.bindValue( ":b", QVariant( QVariant::Int ) );
  query.exec();
  query = QSqlQuery( "SELECT * FROM a", db );
  query.next();
  //  Get value from table - it has type string and is "".
  QVariant val = query.value( query.record().indexOf( "b" ) );
  QVariant::Type type = val.type();
  QString str = val.toString();
}
+2  A: 

In my experience, if the DB value is NULL, QVariant::isNull() will return true with SQLite both on Qt 4.6.2 and Qt 4.6.3.

wellisntTHATqt