Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QRelationalTableModel
Forum Updated to NodeBB v4.3 + New Features

QRelationalTableModel

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rentert
    wrote on last edited by
    #1

    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
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      what is the query you run?

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rentert
        wrote on last edited by
        #3

        [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)!

        1 Reply Last reply
        0
        • C Offline
          C Offline
          clochydd
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved