首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我试图在QT中向QSqlTableModel插入记录时,为什么会出错呢?

当我试图在QT中向QSqlTableModel插入记录时,为什么会出错呢?
EN

Stack Overflow用户
提问于 2021-09-29 19:26:13
回答 1查看 320关注 0票数 0

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

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

代码语言:javascript
复制
/* 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->insertRecord(-1, record);也返回false,而calibration_model->submitAll()返回true

输出:

代码语言:javascript
复制
"ID"
"calibration_id"
"calibration_comment"
"calibration_date_time"
false
"No Fields to update"
Submit:

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

我得到了错误No Fields to update,但这意味着什么?我有一个空桌子,我只想附加一行。

EN

回答 1

Stack Overflow用户

发布于 2021-10-10 18:54:13

不知道你为什么会犯这个错误。我有一个关于QSqlTableModel的小例子。让我把它放在这里。也许你可以和你的代码做比较。

main.cpp

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69382353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档