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. sql request that does not work
Qt 6.11 is out! See what's new in the release blog

sql request that does not work

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.5k 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.
  • E Offline
    E Offline
    EL-jos
    wrote on last edited by EL-jos
    #1

    Hello, everyone,
    I want to make a sql query that can create a table without first knowing the name that this table should have; here is my code:

    int main(int argc, char** argv)
    {
        QCoreApplication app(argc,argv);
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("mydatabase");
        if(db.open()){
    
            qDebug("The connection is established with the database.");
    
            QSqlQuery q1;
            q1.prepare("CREATE TABLE :anonyme (id INT PRIMARY KEY NOT NULL, name VARCHAR(100) )");
            q1.bindValue(":anonyme","MyTable");
            if(q1.exec())
                qDebug("Successful request.");
            else
                qDebug() << "ERREUR: " << q1.lastError().text();
        }else{
    
            qDebug() << " ERREUR: ";
        }
        return app.exec();
    }
    

    I don't know why my code doesn't work.
    and here is the error message: ERREUR: " Parameter count mismatch"
    Thank you for your help.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      I don't see a problem - you can create your statement with the correct table name...
      http://doc.qt.io/qt-5/qstring.html#arg

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        You can only bind parameters, not table names or anything else. Just put 'MyTable' directly in to query.

        CREATE TABLE MyTable (id INT PRIMARY KEY NOT NULL, name VARCHAR(100) )

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • E Offline
          E Offline
          EL-jos
          wrote on last edited by
          #3

          Thank you for your answer;
          Yes, you're right, but I want to create tables without first knowing the name of each one.

          Note: I want the user to enter the name of the table to create

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            I don't see a problem - you can create your statement with the correct table name...
            http://doc.qt.io/qt-5/qstring.html#arg

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3
            • E Offline
              E Offline
              EL-jos
              wrote on last edited by
              #5

              Thank you so much for your help, it works now that God bless you.

              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