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. How to get a list of columns and column types, in the selected table?
Forum Updated to NodeBB v4.3 + New Features

How to get a list of columns and column types, in the selected table?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 6.4k 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.
  • H Offline
    H Offline
    haifisch
    wrote on last edited by
    #1

    How to get a list of columns and column types, in the selected table? And then dynamically add the column name to Label + add LineEdit or Spin Box, depending on the type of column. If the type of the column is Text - add LineEdit, if the type of integer - add Spin Box.

    QHBoxLayout* Layout = new QHBoxLayout;
    Layout->setSizeConstraint(QLayout::SetMaximumSize);
    QSqlQuery* queryTow = new QSqlQuery(conn.mydb);
    QString sqlQueryTow = QString("SELECT column_name,column_type FROM table_name=%1").arg(query->value(0).toString());
    queryTow->prepare(sqlQueryTow);
    queryTow->exec();
    
        while (queryTow->next()) {                
            QLabel *Label = new QLabel(this);                
            QString columnName = queryTow->value(0).toString();
            Label->setText(columnName);
            Layout->addWidget(Label);
            QString columnType = queryTow->value(1).toString();
            if(columnType="TEXT") {
               QLineEdit *LineEdit = new QLineEdit();
               Layout->addWidget(LineEdit);
            } else if (columnType="INTEGER") {
               QSpinBox *SpinBox = new QSpinBox(this);
               Layout->addWidget(SpinBox);
            }
        }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      You probably want QSqlTableModel + QTableView

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      H 2 Replies Last reply
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        As @VRonin says, there are better ways to make something to edit the tables :)

        Anyway, to get the schema you can use sqlite_master
        http://www.sqlite.org/faq.html#q7
        There are similar ways for other DBMS.

        Depending on how exact you need it, you might also be able to use
        http://doc.qt.io/qt-5/qvariant.html#canConvert-1
        on queryTow->value(X) to check what type it can convert to.
        This is however not optimal, but could work if u only look for int/text

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

          If you need the db table types, you can retrieve them via QSqlQuery::record() and then you have access to every column via QSqlRecord::field()/QSqlField::type()

          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
          5
          • VRoninV VRonin

            You probably want QSqlTableModel + QTableView

            H Offline
            H Offline
            haifisch
            wrote on last edited by
            #5

            @VRonin What is the request to the database? To get the names of the columns and the types of columns in the selected table.

            1 Reply Last reply
            0
            • VRoninV VRonin

              You probably want QSqlTableModel + QTableView

              H Offline
              H Offline
              haifisch
              wrote on last edited by
              #6

              @VRonin I use Sqlite.

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

                QSqlRecord also has a function to retrieve the column names ... http://doc.qt.io/qt-5/qsqlfield.html#name

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

                H 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  QSqlRecord also has a function to retrieve the column names ... http://doc.qt.io/qt-5/qsqlfield.html#name

                  H Offline
                  H Offline
                  haifisch
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher To use it you need to get a table with types and names. Let's say I know the name of the table. What query will allow to extract only the names and types of columns in the table?

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

                    Then you should take a look at QSqlDatabase: http://doc.qt.io/qt-5/qsqldatabase.html#record
                    The documentation is your friend

                    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
                    5
                    • H haifisch

                      @Christian-Ehrlicher To use it you need to get a table with types and names. Let's say I know the name of the table. What query will allow to extract only the names and types of columns in the table?

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

                      @haifisch

                      What query will allow to extract only the names and types of columns in the table?

                      In addition to @Christian-Ehrlicher's http://doc.qt.io/qt-5/qsqlfield.html#name for the column names, see http://doc.qt.io/qt-5/qsqlfield.html#type for how the column type is stored at Qt-side. Or, did you want the actual, underlying SQL type?

                      H 1 Reply Last reply
                      2
                      • JonBJ JonB

                        @haifisch

                        What query will allow to extract only the names and types of columns in the table?

                        In addition to @Christian-Ehrlicher's http://doc.qt.io/qt-5/qsqlfield.html#name for the column names, see http://doc.qt.io/qt-5/qsqlfield.html#type for how the column type is stored at Qt-side. Or, did you want the actual, underlying SQL type?

                        H Offline
                        H Offline
                        haifisch
                        wrote on last edited by
                        #11

                        @JonB You can close the question. I already understood how to get a list of columns and column types in the selected table.

                        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