PDA

View Full Version : Set NULL values



LaTj
7th October 2013, 21:48
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

ChrisW67
7th October 2013, 21:55
The underlying database (correctly) treats an empty string as an empty string not a NULL. You insert NULL by using a null QVariant:


tableModel->setData(index, QVariant());