How to get a list of columns and column types, in the selected table?
-
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); } }
-
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 -
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()
-
QSqlRecord also has a function to retrieve the column names ... http://doc.qt.io/qt-5/qsqlfield.html#name
-
QSqlRecord also has a function to retrieve the column names ... http://doc.qt.io/qt-5/qsqlfield.html#name
@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?
-
Then you should take a look at QSqlDatabase: http://doc.qt.io/qt-5/qsqldatabase.html#record
The documentation is your friend -
@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?
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?
-
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?