【问题标题】:Qt sql query failedQt sql查询失败
【发布时间】:2012-05-20 15:12:12
【问题描述】:
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');")

工作正常。

【问题讨论】:

    标签: c++ mysql qt qtsql


    【解决方案1】:

    很难猜出问题所在。但我可以想象的一件事是你的代码肯定有问题的是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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-05
      • 2019-06-18
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      相关资源
      最近更新 更多