If I create a table using Qt and SQLite with zero columns (columns without "NOT NULL") and add a null value to it (using NULL QVariant ), then getting this value will not return, but NULL QVariant , but QVariant type String and values "" . Am I doing something wrong, or Qt with SQLite cannot distinguish between the NULL value in the database and the default value (0, "", ect by default)? My test code is:
#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(); }
source share