Results 1 to 2 of 2

Thread: Set NULL values

  1. #1
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Set NULL values

    I have a simple question for you guys, but confusing to me. I made a database table that looks like this

    CREATE TABLE persinfo (\
    id INTEGER PRIMARY KEY,\
    status VARCHAR(40) NULL,\
    spouse VARCHAR(40) NULL,\
    children VARCHAR(40) NULL,\
    siblings VARCHAR(40) NULL,\
    parents VARCHAR(40) NULL,\
    nameid INTEGER NOT NULL,\
    FOREIGN KEY(nameid) REFERENCES member(id))");

    I am using QSqlTableModel to manipulate the data, but I cannot insert NuLL values for VARCHAR on the above tables using QSqlTableModel::setData();
    I used "" an empty string to insert values like so tableModel->setData(index, "");

    This does not seem right to me and could cause problems later on the database.

    My question is how can yo insert null strings?

    I am open to suggestion using setData, if not just tell me other options like QSqlRecord, or just querying it.

    Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Set NULL values

    The underlying database (correctly) treats an empty string as an empty string not a NULL. You insert NULL by using a null QVariant:
    Qt Code:
    1. tableModel->setData(index, QVariant());
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    LaTj (10th October 2013)

Similar Threads

  1. setRelation and NULL in ID
    By Auryn in forum Qt Programming
    Replies: 4
    Last Post: 26th May 2015, 16:20
  2. How to convert string hex values to its real binary values?
    By AttilaPethe in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 22:47
  3. Null for QDateEdit
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2009, 21:54
  4. QPixmap seems to be null but it isn't
    By satoshi in forum Qt Programming
    Replies: 7
    Last Post: 8th May 2009, 09:23
  5. QSqlTableModel and NULL
    By karas in forum Qt Programming
    Replies: 0
    Last Post: 9th February 2009, 12:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.