Qt sql查询失败

时间:2012-05-12 05:06:20

标签: c++ mysql qt qtsql

query->prepare("INSERT INTO businessweek (id, source, date, headline, body) VALUES (NULL, ?, ?, ?, ?)");
query->bindValue(1, "source");
query->bindValue(2, "date");
query->bindValue(3, "headline");
query->bindValue(4, "body");

if (query->exec()) {
    tt << "Query success";
} else {
    qDebug() << db.lastError();
    return;
}

我正在使用QMYSQL。错误是

 QSqlError(-1, "", "") 

但是

query->exec("INSERT INTO businessweek (id, source, date, headline, body) VALUES (NULL, 'google', 'may 0, 23', 'head', 'fjsdflksjdlkfdsjlfkjd');")

工作正常。

1 个答案:

答案 0 :(得分:3)

很难猜出这个问题。但是我能想象的一件事就是你的代码肯定是错误的,bindValue的字段编号从0开始(见here)。

因此,我建议尝试

query->prepare("INSERT INTO businessweek (id, source, date, headline, body) VALUES (NULL, ?, ?, ?, ?)");
query->bindValue(0, "source");
query->bindValue(1, "date");
query->bindValue(2, "headline");
query->bindValue(3, "body");

if (query->exec()) {
    tt << "Query success";
} else {
    qDebug() << db.lastError();
    return;
}