Solved How to get a list of columns and column types, in the selected table?
-
You probably want
QSqlTableModel
+QTableView
-
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()
-
@VRonin What is the request to the database? To get the names of the columns and the types of columns in the selected table.
-
@VRonin I use Sqlite.
-
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 -
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?
-
@JonB You can close the question. I already understood how to get a list of columns and column types in the selected table.