QRelationalTableModel



  • HI!
    in use at QRelationalTableModel submitAll() and database().transaction() results in an error
    @"ERROR: syntax error at or near "="
    LINE 1: ...REPARE qpsqlpstmt_1 AS UPDATE SET "serial_number"=$1 WHERE ...
    ^
    (42601) QPSQL: Unable to prepare statement"
    "ERROR: syntax error at or near "="
    LINE 1: ...REPARE qpsqlpstmt_1 AS UPDATE SET "serial_number"=$1 WHERE ...
    ^
    (42601) QPSQL: Unable to prepare statement"@
    What's the problem?
    code
    ViewModel.cpp
    @#include "ViewModel.h"
    #include <QTableView>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include "Model/FSqlRelationalTableModel.h"
    #include <QDebug>
    #include <QtSql/QSqlError>

    ViewModel::ViewModel(QString sql, QWidget *parent) :
    QWidget(parent)
    {
    setClassSetting();
    setModel(sql);
    }

    ViewModel::ViewModel(QWidget *parent) :
    QWidget(parent)
    {
    setClassSetting();
    }

    ViewModel::~ViewModel() {
    model -> database().rollback();
    }

    void ViewModel::setClassSetting() {
    table = new QTableView;
    save = new QPushButton("save");
    model = new FSqlRelationalTableModel;
    table -> setModel(model);
    QString text = model -> database().transaction()? "transaction open": "transaction not open";
    qDebug() << text ;
    baseLayout = new QVBoxLayout;
    baseLayout -> addWidget(table);
    baseLayout -> addWidget(save);
    setLayout(baseLayout);

    connect(save, &QPushButton::clicked, this, &ViewModel::slotSave);
    

    }

    void ViewModel::slotSave() {
    //QString text = model -> submitAll()? "model submit": "model not submit";
    QString text = table->model()->submit()? "model submit": "model not submit";
    QSqlError erA = model -> lastError();
    qDebug() << text<< " " << erA.text() ;
    // QString submit = model -> submit()? "is submit": "not submit";
    //qDebug() << submit;
    QSqlError erB = model -> lastError();
    QString string = model -> database().commit()? "commit" : "not commit";
    qDebug() << string << " "<< erB.text();
    }

    void ViewModel::setModel(QString sql) {
    model -> database().rollback();
    model -> sendQuery(sql);
    model -> database().transaction();
    }
    @
    ViewModel.h
    @#ifndef VIEWMODEL_H
    #define VIEWMODEL_H

    #include <QWidget>
    #include <QTableView>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include "Model/FSqlRelationalTableModel.h"

    class ViewModel : public QWidget {
    Q_OBJECT
    public:
    explicit ViewModel(QString sql, QWidget parent = 0);
    explicit ViewModel(QWidget parent = 0);
    ~ViewModel();
    void setModel(QString sql);
    private:
    QTableView
    table;
    QVBoxLayout
    baseLayout;
    QPushButton* save;
    FSqlRelationalTableModel* model;
    void setClassSetting();
    private slots:
    void slotSave();
    };

    #endif // VIEWMODEL_H
    @
    FSqlRelationalTableModel.cpp
    @#include "FSqlRelationalTableModel.h"
    #include <QtSql/QSqlRelationalTableModel>
    #include <QtSql/QSqlQuery>
    /**

    • @brief FSqlRelationalTableModel::FSqlRelationalTableModel
    • @param query
    • @param parent
    • форк QSqlRelationalTableModel
      */
      FSqlRelationalTableModel::FSqlRelationalTableModel(QString query, QObject *parent) :
      QSqlRelationalTableModel(parent)
      {
      sendQuery(query);
      }

    FSqlRelationalTableModel::FSqlRelationalTableModel(QObject parent) :
    QSqlRelationalTableModel(parent)
    {
    }
    /
    *

    • @brief FSqlRelationalTableModel::sendQuery
    • @param sql
      */
      void FSqlRelationalTableModel::sendQuery(QString sql) {
      QSqlQuery query(sql);
      this -> setQuery(query);
      }
      @
      FSqlRelationalTableModel.h
      @#ifndef FSQLRELATIONALTABLEMODEL_H
      #define FSQLRELATIONALTABLEMODEL_H

    #include <QSqlRelationalTableModel>

    class FSqlRelationalTableModel : public QSqlRelationalTableModel {
    Q_OBJECT
    public:
    explicit FSqlRelationalTableModel(QObject *parent = 0);
    explicit FSqlRelationalTableModel(QString query, QObject *parent = 0);
    void sendQuery(QString sql);
    };

    #endif // FSQLRELATIONALTABLEMODEL_H
    @



  • Hi,

    what is the query you run?



  • [quote author="mcosta" date="1397751196"]Hi,

    what is the query you run?[/quote]
    SELECT * FROM Table_name
    my query works fine, as I think
    !http://cs614822.vk.me/v614822621/9bca/1yR6yjlHsww.jpg(program screen)!



  • Hi rentert, I think mcosta asks for the SQL-statement containing ...PREPARE qpsqlpstmt_1 AS UPDATE SET...
    There must be the cause of your issue.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.