3
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');")

工作正常。

4

1 回答 1

3

很难猜出问题所在。但是我可以想象的一件事并且您的代码肯定有问题是字段编号bindValue从 0 开始(请参见此处)。

因此,我建议尝试

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;
}
于 2012-05-12T05:41:06.757 回答