1

For example:

sql = "select a, b, c from tb"
rs = engine.execute(sql)

the columns' types in rs.cursor.description are python types, not sql types, so how can I get columns' sql types?

Jojo
  • 89
  • 8
  • In what case would your program not know what type of data you've queried? You have built / have access to the model definition, so I'm not sure what point it is getting the data type. – ChickenFeet Jun 22 '17 at 08:58
  • See question [Easy convert betwen SQLAlchemy columns and data types?](https://stackoverflow.com/questions/4165143/easy-convert-betwen-sqlalchemy-columns-and-data-types) – ChickenFeet Jun 22 '17 at 09:02
  • Also [SQLAlchemy getting column data types of query results](https://stackoverflow.com/questions/2258072/sqlalchemy-getting-column-data-types-of-query-results) – ChickenFeet Jun 22 '17 at 09:04
  • In a app, the 'sql' is a column of a table in persistent layer database, and need to preview the data queried by the sql showing at the front end. so no more way to get columns' metadata – Jojo Jun 22 '17 at 09:58
  • tried `pandas.read_sql()`, can't get columns' sql types too – Jojo Jun 22 '17 at 10:00
  • You could always map the column names and types to a new table `column_types`. I know this isn't a very clean solution, nor would I recommend it, as if the data type changes and you forget to update the table `column_types` it will break the commit. But if you're struggling to find any solution and the types in the database aren't going to change, then storing the column_name / type in a kind of associate table should solve the problem. – ChickenFeet Jun 23 '17 at 02:11

0 Answers0