Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. MYSQL with Qt query and with var [solved]
QtWS25 Last Chance

MYSQL with Qt query and with var [solved]

Scheduled Pinned Locked Moved C++ Gurus
9 Posts 2 Posters 4.0k 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.
  • N Offline
    N Offline
    nickvan86
    wrote on last edited by
    #1

    Hello Forum,

    i have a question about MYSQL Syntax in Qt. I have a table with the name “min5_1” an i have problems with the query.

    The Code:

    @ MainWindow main;
    this->comboxLaden();
    QString tableName = ui->tablesBox->currentText();
    qDebug() << tableName;
    QSqlQueryModel *model = new QSqlQueryModel();
    QSqlQuery *qry = new QSqlQuery(main.mydb1);
    qry->prepare("SELECT * FROM '"+tableName+"' ");
    if(qry->exec())
    {
    qDebug() << "If schleife";
    model->setQuery(*qry);
    ui->anzeigenView->setModel(model);
    }
    else
    qDebug() << "Else" << qry->exec();@

    The exec is always false. When i changed the var “tableName” to “min5_1” the query is true. What is wrong?

    Alex

    Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

    1 Reply Last reply
    0
    • W Offline
      W Offline
      wlinaz
      wrote on last edited by
      #2

      My first thought was a 'locked' database but hard coding the tablename shouldn't cause a difference.
      Try adding qDebug() << qry.lastError();
      after qry->prepare statement
      and after qry->exec statement
      See what pops up. Chasing these can be fun.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wlinaz
        wrote on last edited by
        #3

        Sorry qry->lastError();

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nickvan86
          wrote on last edited by
          #4

          The query->lastError(); is:

          bq. QSqlError(1064, "QMYSQL: Unable to execute query", "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''min5_1'' at line 1")

          And the Code is:

          @MainWindow main;
          this->comboxLaden();
          QString tableName = ui->tablesBox->currentText();
          QSqlQueryModel *model = new QSqlQueryModel();
          QSqlQuery *qry = new QSqlQuery(main.mydb1);
          qry->prepare("SELECT * FROM '"+tableName+"' ");
          qry->exec();
          qDebug() << qry->lastError();
          model->setQuery(*qry);
          ui->anzeigenView->setModel(model);@

          I´dont understand this. When i change the prepare with var to const string than it works correctly.

          Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

          1 Reply Last reply
          0
          • W Offline
            W Offline
            wlinaz
            wrote on last edited by
            #5

            I think I would try:
            QString sstr = ("SELECT * FROM '"+tableName+"' ");
            qry->prepare(sstr); ...
            You need to see what is actually passed to the prepare statement.
            I also noticed in copying your line that there is a space after the final single quote. This may be the issue.

            1 Reply Last reply
            0
            • W Offline
              W Offline
              wlinaz
              wrote on last edited by
              #6

              Sorry I didn't use code quotes,
              @QString sstr = (“SELECT * FROM ‘“tableName”’ “);@

              1 Reply Last reply
              0
              • W Offline
                W Offline
                wlinaz
                wrote on last edited by
                #7

                Another try...

                @QString sstr = ("SELECT * FROM '"+tableName+"' ")@
                But I'm guessing you got the idea...

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  nickvan86
                  wrote on last edited by
                  #8

                  Thank you. I have the right code:

                  @ MainWindow main;
                  this->comboxLaden();
                  QString tableName = ui->tablesBox->currentText();
                  QString stringQuery = "select * from "+tableName+"";
                  QSqlQueryModel *model = new QSqlQueryModel();
                  QSqlQuery *qry = new QSqlQuery(main.mydb1);
                  qry->prepare(stringQuery);
                  qry->exec();
                  qDebug() << stringQuery;
                  model->setQuery(*qry);
                  ui->anzeigenView->setModel(model);@

                  It works correctly

                  Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    nickvan86
                    wrote on last edited by
                    #9

                    And the other code:

                    @qry->prepare("select * from "+tableName+"");@

                    Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

                    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