Qt6 topics merged with General

[SOLVED] Inserting zero (0) to SQLite database

  • Hello,

    I have a problem with inserting a zero value to QSqlDatabase. So, basicly, if I:

    @this->record->setValue(index, QVariant(0));@

    And when I submit this to database later on, it inserts the value as NULL, empty column. I've tried:

    QString tmp = "0";
    this->record->setValue(index, QVariant(tmp));

    But it doesn't matter. I guess QVariant casts the value to NULL on it's constructor, is there any way to change this behavior?

  • Anyone? I've noticed, that if I update an existing row, it does insert 0. Pretty weird, they're called with pretty much same method on my SQL-table class..

    EDIT: I've now debugged the reason for this, and it's because of this:

    bool QSqlTableModel::setData documentation:
    Returns true if value is equal to the current value. However, the value will not be submitted to the database.

    So, when the current value is empty in the database (= when a new record is inserted), it checks NULL == 0, and doesn't insert the 0 in the database.. Is there any way to change this behavior, does anyone know? Can I somehow override this check in setData -function?

  • Ahh, this seems to have been fixed in Qt 5.1.1 :) Great! Problem solved.

Log in to reply