【问题标题】:Why do I get false when I try to insert a record to a QSqlTableModel in QT?当我尝试在 QT 中向 QSqlTableModel 插入记录时,为什么会出现错误?
【发布时间】:2021-09-29 19:26:13
【问题描述】:

我正在创建一个QSqlRecord 对象,然后将值设置为该QSqlRecord 对象。但是即使我将QSqlRecord对象插入QSqlTableModel对象,插入记录的函数也会返回false。

我有这个 C++ 代码,它创建了一个 QSqlRecord 对象并设置了值。它以正确的索引顺序设置值作为表的创建方式。

/* Insert data */
int column_index = 0; /* Index 0 is the ID  column */
QSqlRecord record;
qDebug() << CALIBRATION_COLUMNS.at(column_index).first;
record.setValue(CALIBRATION_COLUMNS.at(column_index++).first, 1); /* ID */
qDebug() << CALIBRATION_COLUMNS.at(column_index).first;
record.setValue(CALIBRATION_COLUMNS.at(column_index++).first, calibration_id);
qDebug() << CALIBRATION_COLUMNS.at(column_index).first;
record.setValue(CALIBRATION_COLUMNS.at(column_index++).first, calibration_comment);
qDebug() << CALIBRATION_COLUMNS.at(column_index).first;
record.setValue(CALIBRATION_COLUMNS.at(column_index++).first, calibration_date_time);
for(int i = 0; i < 12; i++){
    record.setValue(CALIBRATION_COLUMNS.at(column_index++).first, min_adc[i]);
    record.setValue(CALIBRATION_COLUMNS.at(column_index++).first, max_adc[i]);
    record.setValue(CALIBRATION_COLUMNS.at(column_index++).first, bias_adc[i]);
}
for(int i = 0; i < 5; i++){
    record.setValue(CALIBRATION_COLUMNS.at(column_index++).first, min_dadc[i]);
    record.setValue(CALIBRATION_COLUMNS.at(column_index++).first, max_dadc[i]);
    record.setValue(CALIBRATION_COLUMNS.at(column_index++).first, bias_dadc[i]);
}
for(int i = 0; i < 2; i++)
    record.setValue(CALIBRATION_COLUMNS.at(column_index++).first, pulses_per_revolution_encoder[i]);

/* -1 means append record */
qDebug() << calibration_model->insertRecord(-1, record);
qDebug() << calibration_model->lastError().text();
qDebug() << "Submit:";
if(!calibration_model->submitAll()){
    qDebug() << calibration_model->lastError().text();
    return DATABASE_STATUS_COULD_NOT_INSERT_ROW;
}
return DATABASE_STATUS_OK;

但是即使我插入记录,这个函数calibration_model-&gt;insertRecord(-1, record);返回false但是calibration_model-&gt;submitAll()返回true

输出:

"ID"
"calibration_id"
"calibration_comment"
"calibration_date_time"
false
"No Fields to update"
Submit:

那么告诉我。我在这里做错了什么?

我收到错误No Fields to update,但这是什么意思?我有一个空表,我只想追加一行。

【问题讨论】:

  • 您使用的是哪个版本的 QT?这是 Qt 4.4.1 和 4.4.2 中的错误;
  • @VahagnAvagyan QT 5.1.12
  • @VahagnAvagyan 是的!我正在尝试改用 QSqlQuery 类。效果更好。
  • QSqlRecord record;改成QSqlRecord record = calibration_model-&gt;record()

标签: qt qt5 qtsql


【解决方案1】:

不确定您为什么会收到该错误。我有一个QSqlTableModel 的小例子。让我把它放在这里。也许您可以与您的代码进行比较。

ma​​in.cpp

#include <QApplication>
#include "mysqltablemodel.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("mydb");

    if(!db.open()) {
        qDebug() << db.lastError().text();
        return 0;
    }

    QSqlQuery query(db);

    if(!query.exec("DROP TABLE IF EXISTS mytable")) {
        qDebug() << "create table error: " << query.lastError().text();
        return 0;
    }

    if(!query.exec("CREATE TABLE IF NOT EXISTS mytable \
                   (id integer primary key autoincrement, name varchar(15), salary integer)")) {
        qDebug() << "create table error: " << query.lastError().text();
        return 0;
    }

    MySqlTableModel *model = new MySqlTableModel(0, db);
    model->setTable("mytable");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();

    QSqlRecord rec = model->record();
    rec.setValue(1, "peter");
    rec.setValue(2, 100);
    qDebug() << model->insertRecord(-1, rec);
    rec.setValue(1, "luke");
    rec.setValue(2, 200);
    qDebug() << model->insertRecord(-1, rec);

    if(model->submitAll()) {
        model->database().commit();
    } else {
        model->database().rollback();
        qDebug() << "database error: " << model->lastError().text();
    }

    query.exec("SELECT name, salary FROM mytable");

    while (query.next()){
        QString name = query.value(0).toString();
        int salary = query.value(1).toInt();
        qDebug() << name << salary;
    }

    return app.exec();
}

mysqltablemodel.h

#ifndef MYSQLTABLEMODEL_H
#define MYSQLTABLEMODEL_H

#include <QSqlTableModel>
#include <QSqlRecord>
#include <QSqlError>
#include <QSqlQuery>
#include <QDebug>

class MySqlTableModel : public QSqlTableModel
{
    Q_OBJECT

public:
    MySqlTableModel(QObject *parent = 0, QSqlDatabase db = QSqlDatabase());
    QVariant data(const QModelIndex &index, int role=Qt::DisplayRole ) const;

protected:
    QHash<int, QByteArray> roleNames() const;

private:
    QHash<int, QByteArray> roles;
};

#endif // MYSQLTABLEMODEL_H

mysqltablemodel.cpp

#include "mysqltablemodel.h"

MySqlTableModel::MySqlTableModel(QObject *parent, QSqlDatabase db): QSqlTableModel(parent, db) {}

QVariant MySqlTableModel::data ( const QModelIndex & index, int role ) const
{
    if(index.row() >= rowCount()) {
        return QString("");
    }
    if(role < Qt::UserRole) {
        return QSqlQueryModel::data(index, role);
    }
    else {
        return QSqlQueryModel::data(this->index(index.row(), role - Qt::UserRole), Qt::DisplayRole);
    }
}

QHash<int, QByteArray> MySqlTableModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[Qt::UserRole + 1] = "name";
    roles[Qt::UserRole + 2] = "salary";
    return roles;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2012-06-14
    相关资源
    最近更新 更多