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. Create new table and naming it with user input from QLineEdit
Forum Updated to NodeBB v4.3 + New Features

Create new table and naming it with user input from QLineEdit

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.2k 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.
  • G Offline
    G Offline
    GARUCHIRAZUMAN
    wrote on last edited by
    #1

    Im making a MYSQL database using QT5 on my raspberry pi. Already made all app and localhost and everything ok so far. My question is on how to create a new table named by user from a qlineEdit on the UI, when pressing a button?

    Here the code I have for the button slot:

    void MainWindow::on_pushButton_clicked()
    {
        QSqlQuery query;
        query.prepare( "CREATE TABLE `QT`.`tlb` ( `id` INT NOT NULL AUTO_INCREMENT , `desde` 
        VARCHAR(10) NOT NULL , `hasta` VARCHAR(10) NOT NULL , `name` VARCHAR(50) NOT NULL , 
        `color` TEXT NOT NULL , `circuits_qty` INT(65) NOT NULL , `customer` TEXT NULL , 
        `program` TEXT NULL , `family` TEXT NULL , `location` VARCHAR(40) NULL , `con1` 
        LONGBLOB NULL , `con2` LONGBLOB NULL , `con3` LONGBLOB NULL , `con4` LONGBLOB NULL , 
        `con5` LONGBLOB NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB");
        
        QString name;
        name = ui->lineEdit->text();
        query.bindValue(":'tlb', 'name'");
        // query.prepare("ALTER TABLE 'tlb' RENAME TO name;)
        query.exec();
    }
    

    As you can see, code works when just giving a name by me. But when trying to make the user give the name to the table, nothing happens. I made a QString to get the name from the qlineEdit store the name then, tried to bind it, but did not work, then tried an alter table but still nothing happened. Any help is more than welcome. Commented out the alter table btw just to go back and forward trying different ways to get this works.

    artwawA jsulmJ JonBJ 4 Replies Last reply
    0
    • G GARUCHIRAZUMAN

      Im making a MYSQL database using QT5 on my raspberry pi. Already made all app and localhost and everything ok so far. My question is on how to create a new table named by user from a qlineEdit on the UI, when pressing a button?

      Here the code I have for the button slot:

      void MainWindow::on_pushButton_clicked()
      {
          QSqlQuery query;
          query.prepare( "CREATE TABLE `QT`.`tlb` ( `id` INT NOT NULL AUTO_INCREMENT , `desde` 
          VARCHAR(10) NOT NULL , `hasta` VARCHAR(10) NOT NULL , `name` VARCHAR(50) NOT NULL , 
          `color` TEXT NOT NULL , `circuits_qty` INT(65) NOT NULL , `customer` TEXT NULL , 
          `program` TEXT NULL , `family` TEXT NULL , `location` VARCHAR(40) NULL , `con1` 
          LONGBLOB NULL , `con2` LONGBLOB NULL , `con3` LONGBLOB NULL , `con4` LONGBLOB NULL , 
          `con5` LONGBLOB NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB");
          
          QString name;
          name = ui->lineEdit->text();
          query.bindValue(":'tlb', 'name'");
          // query.prepare("ALTER TABLE 'tlb' RENAME TO name;)
          query.exec();
      }
      

      As you can see, code works when just giving a name by me. But when trying to make the user give the name to the table, nothing happens. I made a QString to get the name from the qlineEdit store the name then, tried to bind it, but did not work, then tried an alter table but still nothing happened. Any help is more than welcome. Commented out the alter table btw just to go back and forward trying different ways to get this works.

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      @GARUCHIRAZUMAN I never used query.prepare or bind values but in general you can substitute in two ways:

      1. Less elegant way: query.exec("create table "+ui->lineEdit->text()+"(id integer primary key, something varchar(100));"); That is rather lazy way and I would not recommend.
      2. Proper substitution:
      QString q = QString("create table %1(id integer primary key, something varchar(100));").arg(ui->lineEdit-text());
      query.exec(q);
      

      Also, when you need to specify text in the db query params take form '%1' since % parameters just insert literal.
      Please read QString and QSqlQuery documentation for your version of Qt - I know that is a lot of reading but it is totally worth it.

      For more information please re-read.

      Kind Regards,
      Artur

      G 1 Reply Last reply
      3
      • G GARUCHIRAZUMAN

        Im making a MYSQL database using QT5 on my raspberry pi. Already made all app and localhost and everything ok so far. My question is on how to create a new table named by user from a qlineEdit on the UI, when pressing a button?

        Here the code I have for the button slot:

        void MainWindow::on_pushButton_clicked()
        {
            QSqlQuery query;
            query.prepare( "CREATE TABLE `QT`.`tlb` ( `id` INT NOT NULL AUTO_INCREMENT , `desde` 
            VARCHAR(10) NOT NULL , `hasta` VARCHAR(10) NOT NULL , `name` VARCHAR(50) NOT NULL , 
            `color` TEXT NOT NULL , `circuits_qty` INT(65) NOT NULL , `customer` TEXT NULL , 
            `program` TEXT NULL , `family` TEXT NULL , `location` VARCHAR(40) NULL , `con1` 
            LONGBLOB NULL , `con2` LONGBLOB NULL , `con3` LONGBLOB NULL , `con4` LONGBLOB NULL , 
            `con5` LONGBLOB NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB");
            
            QString name;
            name = ui->lineEdit->text();
            query.bindValue(":'tlb', 'name'");
            // query.prepare("ALTER TABLE 'tlb' RENAME TO name;)
            query.exec();
        }
        

        As you can see, code works when just giving a name by me. But when trying to make the user give the name to the table, nothing happens. I made a QString to get the name from the qlineEdit store the name then, tried to bind it, but did not work, then tried an alter table but still nothing happened. Any help is more than welcome. Commented out the alter table btw just to go back and forward trying different ways to get this works.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #3

        @GARUCHIRAZUMAN said in Create new table and naming it with user input from QLineEdit:

        query.bindValue(":'tlb', 'name'");

        What is this?!
        Makes no sense.
        Should be:

        query.bindValue(":tlb", name);
        

        You can see how it is done in the documentation: https://doc.qt.io/qt-5/qsqlquery.html

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • G GARUCHIRAZUMAN

          Im making a MYSQL database using QT5 on my raspberry pi. Already made all app and localhost and everything ok so far. My question is on how to create a new table named by user from a qlineEdit on the UI, when pressing a button?

          Here the code I have for the button slot:

          void MainWindow::on_pushButton_clicked()
          {
              QSqlQuery query;
              query.prepare( "CREATE TABLE `QT`.`tlb` ( `id` INT NOT NULL AUTO_INCREMENT , `desde` 
              VARCHAR(10) NOT NULL , `hasta` VARCHAR(10) NOT NULL , `name` VARCHAR(50) NOT NULL , 
              `color` TEXT NOT NULL , `circuits_qty` INT(65) NOT NULL , `customer` TEXT NULL , 
              `program` TEXT NULL , `family` TEXT NULL , `location` VARCHAR(40) NULL , `con1` 
              LONGBLOB NULL , `con2` LONGBLOB NULL , `con3` LONGBLOB NULL , `con4` LONGBLOB NULL , 
              `con5` LONGBLOB NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB");
              
              QString name;
              name = ui->lineEdit->text();
              query.bindValue(":'tlb', 'name'");
              // query.prepare("ALTER TABLE 'tlb' RENAME TO name;)
              query.exec();
          }
          

          As you can see, code works when just giving a name by me. But when trying to make the user give the name to the table, nothing happens. I made a QString to get the name from the qlineEdit store the name then, tried to bind it, but did not work, then tried an alter table but still nothing happened. Any help is more than welcome. Commented out the alter table btw just to go back and forward trying different ways to get this works.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @GARUCHIRAZUMAN
          Further to the points from @artwaw & @jsulm.

          I believe you will find that not all SQL statements allow for variable binding. I do not know how/where you discover which statements do/do not, but CREATE TABLE may be one which does not allow the table name to come from a bound variable. If that is the case you would need to construct your statement like @artwaw showed in his examples.

          G 1 Reply Last reply
          1
          • G GARUCHIRAZUMAN

            Im making a MYSQL database using QT5 on my raspberry pi. Already made all app and localhost and everything ok so far. My question is on how to create a new table named by user from a qlineEdit on the UI, when pressing a button?

            Here the code I have for the button slot:

            void MainWindow::on_pushButton_clicked()
            {
                QSqlQuery query;
                query.prepare( "CREATE TABLE `QT`.`tlb` ( `id` INT NOT NULL AUTO_INCREMENT , `desde` 
                VARCHAR(10) NOT NULL , `hasta` VARCHAR(10) NOT NULL , `name` VARCHAR(50) NOT NULL , 
                `color` TEXT NOT NULL , `circuits_qty` INT(65) NOT NULL , `customer` TEXT NULL , 
                `program` TEXT NULL , `family` TEXT NULL , `location` VARCHAR(40) NULL , `con1` 
                LONGBLOB NULL , `con2` LONGBLOB NULL , `con3` LONGBLOB NULL , `con4` LONGBLOB NULL , 
                `con5` LONGBLOB NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB");
                
                QString name;
                name = ui->lineEdit->text();
                query.bindValue(":'tlb', 'name'");
                // query.prepare("ALTER TABLE 'tlb' RENAME TO name;)
                query.exec();
            }
            

            As you can see, code works when just giving a name by me. But when trying to make the user give the name to the table, nothing happens. I made a QString to get the name from the qlineEdit store the name then, tried to bind it, but did not work, then tried an alter table but still nothing happened. Any help is more than welcome. Commented out the alter table btw just to go back and forward trying different ways to get this works.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @GARUCHIRAZUMAN One more thing: you can check how the executed query looks like: https://doc.qt.io/qt-5/qsqlquery.html#executedQuery
            And please also check the errors: https://doc.qt.io/qt-5/qsqlquery.html#lastError

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            G 1 Reply Last reply
            2
            • artwawA artwaw

              @GARUCHIRAZUMAN I never used query.prepare or bind values but in general you can substitute in two ways:

              1. Less elegant way: query.exec("create table "+ui->lineEdit->text()+"(id integer primary key, something varchar(100));"); That is rather lazy way and I would not recommend.
              2. Proper substitution:
              QString q = QString("create table %1(id integer primary key, something varchar(100));").arg(ui->lineEdit-text());
              query.exec(q);
              

              Also, when you need to specify text in the db query params take form '%1' since % parameters just insert literal.
              Please read QString and QSqlQuery documentation for your version of Qt - I know that is a lot of reading but it is totally worth it.

              G Offline
              G Offline
              GARUCHIRAZUMAN
              wrote on last edited by
              #6

              @artwaw Thank you So much! Problem solved with both ways. I also spent sometime reading about QString and QSqlQuery (can't believe that QString has such a world of functions) and for now no more issues to keep progressing on my projects, thanks to you !

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @GARUCHIRAZUMAN One more thing: you can check how the executed query looks like: https://doc.qt.io/qt-5/qsqlquery.html#executedQuery
                And please also check the errors: https://doc.qt.io/qt-5/qsqlquery.html#lastError

                G Offline
                G Offline
                GARUCHIRAZUMAN
                wrote on last edited by
                #7

                @jsulm Indeed, I corrected the errors and followed @artwaw 's advise and works as a charm. Thank you for helping me too !

                1 Reply Last reply
                0
                • JonBJ JonB

                  @GARUCHIRAZUMAN
                  Further to the points from @artwaw & @jsulm.

                  I believe you will find that not all SQL statements allow for variable binding. I do not know how/where you discover which statements do/do not, but CREATE TABLE may be one which does not allow the table name to come from a bound variable. If that is the case you would need to construct your statement like @artwaw showed in his examples.

                  G Offline
                  G Offline
                  GARUCHIRAZUMAN
                  wrote on last edited by
                  #8

                  @JonB Yes, I followed @artwaw example and worked perfect. Thank you for helping me too !

                  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