Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QSqlQuery,当QLineEdit为空时绑定null_C++_Sql_Qt - Fatal编程技术网

C++ QSqlQuery,当QLineEdit为空时绑定null

C++ QSqlQuery,当QLineEdit为空时绑定null,c++,sql,qt,C++,Sql,Qt,我正在处理一个数据库应用程序,当QLinEdit为空时,需要向数据库写入一个NULL值(与空白相对,空白似乎是默认行为) 尽管以下代码总是将NULL写入数据库,即使QLinEdit中包含文本字符串,但它仍能正常工作。非常感谢您的帮助 示例代码: QSqlQuery qry; qry.prepare("insert into [DB] (BUSN_MNGR) values (:BUSN_MNGR)"); QString BUSN_MNGR; BUSN_MNGR=ui->txt_BUSN

我正在处理一个数据库应用程序,当
QLinEdit
为空时,需要向数据库写入一个
NULL
值(与空白相对,空白似乎是默认行为)

尽管以下代码总是将
NULL
写入数据库,即使
QLinEdit
中包含文本字符串,但它仍能正常工作。非常感谢您的帮助

示例代码:

QSqlQuery qry;

qry.prepare("insert into [DB] (BUSN_MNGR) values (:BUSN_MNGR)");

QString BUSN_MNGR;

BUSN_MNGR=ui->txt_BUSN_MNGR->text();

qry.bindValue("BUSN_MNGR", BUSN_MNGR.isEmpty() ? QVariant(QVariant::String): BUSN_MNGR);  // bind null if the string is empty

qry.exec .... etc. 

由于某些原因,只有
null
被传递到数据库

根据文档,您需要在占位符名称的开头包含冒号:

所以看起来更像这样:

qry.bindValue(":BUSN_MNGR", /* ... */);
谢谢你,亲爱的

如果其他人感兴趣,我通过以下代码调整解决了我的问题:

 qry.bindValue(":BUSN_MNGR", ui->txt_BUSN_MNGR->text().trimmed().isEmpty() ? QVariant(QVariant::String): ui->txt_BUSN_MNGR->text());