if(isConnectDatabase())
{
QString patientName = d.dirName();
QSqlQuery findQuery;
findQuery.prepare("SELECT * FROM datamanage.patients WHERE PatientName = ?");
findQuery.bindValue(":PatientName",patientName);
findQuery.exec();
//int PatientId = findQuery.value(0).toInt(); //放在这里出错
if(findQuery.next())
{
int PatientId = findQuery.value(0).toInt(); //正确
QString name = findQuery.value(1).toString();
}
}
QSqlQuery返回的数据集,record是停在第一条记录之前的。所以,你获得数据集后,必须执行next()或first()到第一条记录,这时候record才是有效的。否则,exec完直接value肯定报这个错