Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Preparing query for QSqlQUeryModel
Forum Update on Monday, May 27th 2025

Preparing query for QSqlQUeryModel

Scheduled Pinned Locked Moved Language Bindings
10 Posts 3 Posters 5.9k Views
  • 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.
  • S Offline
    S Offline
    sax94
    wrote on 18 Feb 2013, 11:14 last edited by
    #1

    hy to all
    I'm trying to prepare a query and then to bind new values to it.
    this is what I managed to do
    @
    QSqlQuery query;
    query.prepare("INSERT INTO cliente(calo, titoloargento, nomecliente) "
    "VALUES (?, ?, ?)");
    query.addBindValue(ui->doubleSpinBox_calo->value());
    query.addBindValue(ui->spinBox->value());
    query.addBindValue(ui->lineEdit_nomeCliente->text());
    query.exec()
    @
    and this works fine, now I want to retrieve info from the database, and I'm using QSqlQueryModel. It works fine until this point:
    @
    this->model=new QSqlQueryModel();
    model->setQuery("SELECT nomecliente FROM cliente");
    ui->tableView->setVisible(true);
    ui->tableView->setModel(model);
    @
    but when I want to add a WHERE clause with values taken from a label I can't manage. I've tried this:
    @
    this->model=new QSqlQueryModel();
    QSqlQuery query;
    query.prepare("SELECT nomecliente FROM cliente WHERE nomeclient=?");
    query.addBindValue(ui->lineEdit_6->text());
    model->setQuery(query);
    ui->tableView->setVisible(true);
    ui->tableView->setModel(model);
    @
    but nothing happens...
    anybody has some suggestions??

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on 18 Feb 2013, 11:26 last edited by
      #2

      Put after model->setQuery(query);

      @if (model.lastError().isValid())
      qDebug() << model.lastError();@
      and look what error it returns

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on 18 Feb 2013, 11:28 last edited by
        #3

        To moderators: this topic not belong to Language Bindings, can you move it to General and Desktop?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 18 Feb 2013, 11:37 last edited by
          #4

          For such a simple query you could also use:
          @@ QSqlQuery query(QString("SELECT nomecliente FROM cliente WHERE nomeclient='%1'").arg(ui->lineEdit_6->text());

          Hope it helps

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on 18 Feb 2013, 11:52 last edited by
            #5

            For more simplicity you also can write like this:
            @model->setQuery(QString(“SELECT nomecliente FROM cliente WHERE nomeclient=’%1’”).arg(ui->lineEdit_6->text());@
            But SGaist don't you think if query need some parameters then prepare() is prefered?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sax94
              wrote on 18 Feb 2013, 16:28 last edited by
              #6

              hy, thank you for replying , qxoz
              @
              if (model.lastError().isValid())
              qDebug() << model.lastError();
              @
              first gave me back a very stupid error that I did, I had the column name wrong, I fixed it but is still not working and your piece of code isn't returning any errors.
              for what it concernes the other piece of codeqt gives me this errore : error: stray '\200' in program it gives it to me times and the number changes quite often

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 18 Feb 2013, 16:44 last edited by
                #7

                @qxoz IMHO It really depends on the use case.

                @sax94: I've had this from time to time when I copy pasted code, sometimes it copied some invisible char that went in the file. The only thing i found was to delete the lines surrounding the stray source and rewriting it. You might want to try a replace with a RegExp.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sax94
                  wrote on 19 Feb 2013, 10:48 last edited by
                  #8

                  thank's it worked fine

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    qxoz
                    wrote on 19 Feb 2013, 11:19 last edited by
                    #9

                    What was wrong? Invisible char?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sax94
                      wrote on 19 Feb 2013, 15:27 last edited by
                      #10

                      yes!!

                      1 Reply Last reply
                      0

                      2/10

                      18 Feb 2013, 11:26

                      8 unread
                      • Login

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