0

I've seen a few posts on this but nothing that works for me unfortunately.

Basically trying to get the SQLAlchemy (or pandas) column data types from a list of views in a MySQL database.

import sqlalchemy as sa
view = "myView"
engine = "..."
meta = sa.MetaData(engine, True)

This errors:

tb_data = meta.tables["tb_data"]
# KeyError: 'tb_data'

And I don't know what I'm supposed to do with this:

sa.Table(view, meta).columns
# <sqlalchemy.sql.base.ImmutableColumnCollection at 0x7f9cb264d4a0>

Saw this somewhere but not sure how I'm supposed to use it:

str_columns = filter(lambda column: isinstance(column.type, sa.TEXT), columns)
# <filter at 0x7f9caafab640>

Eventually what I'm trying to achieve is a list or dict of data types for a view that I can then use to load to a PostgreSQL database. Happy to consider alternatives outside of sqlalchemy and/or pandas if they exist (and are relatively trivial to implement).

AK91
  • 671
  • 2
  • 13
  • 35
  • quick clarification question -- what do you expect `meta.tables["tb_data"]` to give you? `meta.tables` should give a listing of tables in a dictionary-like format, but you would be expected to use the `sqlalchemy` methods to manipulate it. Maybe this will help? https://stackoverflow.com/questions/6473925/sqlalchemy-getting-a-list-of-tables – richyen Sep 14 '22 at 07:08

0 Answers0