开发者

Qt and SQLite can't handle 'NULL' values or i'm doning something wrong?

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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜