Qt Sqlite更新返回false

时间:2018-12-06 12:27:33

标签: c++ qt sqlite

我有一个插入行的方法,它工作正常,但是我的更新方法在“ query.exec()”处返回false。这是我的代码。提前谢谢

bool DbManager::InsertPerson(QString name,QString surname,int age,QDate birthDate){

    if(openDB()){
        QSqlQuery query;
        query.prepare("INSERT INTO PERSON (FIRSTNAME, LASTNAME,AGE,BIRTHDATE) "
                      "VALUES (:FIRSTNAME, :LASTNAME, :AGE, :BIRTHDATE)");
        query.bindValue(":FIRSTNAME", name);
        query.bindValue(":LASTNAME", surname);
        query.bindValue(":AGE", age);
        query.bindValue(":BIRTHDATE",birthDate.toString("yyyy-MM-dd").append(" 00:00:00.000"));
        return query.exec();
    }else
    {
        return false;
    }
}

bool DbManager::UpdatePerson(int id, QString name,QString surname,int age,QDate birthDate){

    if(openDB()){
        QSqlQuery query;
        query.prepare("UPDATE PERSON SET FIRSTNAME:FIRSTNAME, LASTNAME=:LASTNAME,AGE=:AGE,BIRTHDATE=:BIRTHDATE WHERE ID=:ID");
        query.bindValue(":FIRSTNAME", name);//VARCHAR(20)
        query.bindValue(":LASTNAME", surname);//VARCHAR(30)
        query.bindValue(":AGE", age);//INTEGER
        query.bindValue(":BIRTHDATE",birthDate.toString("yyyy-MM-dd").append(" 00:00:00.000"));//TEXT:yyyy-MM-dd HH:MI:SS.SSS
        query.bindValue(":ID", id);//INTEGER

        return query.exec();
    }else
    {
        return false;
    }
}

1 个答案:

答案 0 :(得分:1)

我发现了问题。我必须在FIRSTNAME的“:”之前添加“ =“。”

更新人员设置FIRSTNAME:FIRSTNAME