asit_dhal
asit_dhal

Reputation: 1269

column names and types for insert operation in sqlalchemy

I am building a sqlite browser in Python/sqlalchemy.

Here is my requirement. I want to do insert operation on the table. I need to pass a table name to a function. It should return all columns along with the respective types.

Can anyone tell me how to do this in sqlalchemy ?

Upvotes: 1

Views: 204

Answers (1)

javex
javex

Reputation: 7544

You can access all columns of a Table like this:

my_table.c

Which returns a type that behaves similar to a dictionary, i.e. it has values method and so on:

columns = [(item.name, item.type) for item in my_table.c.values()]

You can play around with that to see what you can get from that. Using the declarative extension you can access the table through the class' __table__ attribute. Furthermore, you might find the Runtime Inspection API helpful.

Upvotes: 1

Related Questions