简体   繁体   English

使用Flask SQLAlchemy从MySQL表访问所有列

[英]Accessing all columns from MySQL table using flask sqlalchemy

I'm trying to get the columns for a MySQL table whether in a string or a list format. 我正在尝试以字符串或列表格式获取MySQL表的列。

Since I defined the table through my main app, I could use dir(table_name) to get a list of attributes but those contain private attributes and other built-in attributes like "query" or "query_class". 由于我是通过主应用程序定义表的,因此可以使用dir(table_name)获取属性列表,但这些属性包含私有属性和其他内置属性,例如“ query”或“ query_class”。 Filtering these would be possible but I'm trying to find an easier way to get the columns without going through the attribute route. 可以对它们进行过滤,但是我试图找到一种更简单的方法来获取列而不通过属性路由。

Between reading the flask-sqlalchemy documentation and sqlalchemy documentation, I noticed that using table_name.c will work for sqlalchelmy. 在阅读了flask-sqlalchemy文档和sqlalchemy文档之间,我注意到使用table_name.c可以用于sqlalchelmy。 Is there an equivalent for flask-sqlalchemy? flask-sqlalchemy是否有等效项?

Flask-SQLAlchemy does nothing to the SQLAlchemy side of things, it is just a wrapper to make it easier to use with Flask along with some convenience features. Flask-SQLAlchemy对SQLAlchemy方面没有任何作用,它只是一个包装,使它更易于与Flask一起使用以及一些便利功能。 You can inspect orm objects the same way you normally would. 您可以像平常一样检查orm对象 For example, inspecting a model returns its mapper . 例如, 检查模型将返回其mapper

m = db.inspect(MyModel)
# all orm attributes
print(list(m.all_orm_descriptors.keys()))
# just the columns
print(list(m.columns.keys()))
# etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask-SQLAlchemy连接是否可以从一个表中选择所有列,并从另一个表中指定一些列? - Is it possible with Flask-SQLAlchemy join to select all columns from one table and specify some from another? 在表中添加多列,例如在flask-sqlalchemy 中使用for / while 循环 - Adding multiple columns in a table such as using a for / while loop in flask-sqlalchemy 使用flask-sqlalchemy查询数据库中的现有mysql表 - Query an existing mysql table in database using flask-sqlalchemy 从表中获取所有关系的列表 Flask SQLalchemy - Get list of all relationships from a table Flask SQLalchemy 在 python 上使用 Python、Flask、Sqlalchemy 和 Z4C4AD5FCA2E7A3F74DBBanywhereCED0Z 显示数据库中的表 - Displaying Table from Database using Python, Flask, Sqlalchemy, and HTML on pythonanywhere 从 SqlAlchemy 表中获取所有列以对其进行过滤 - Get all columns from a SqlAlchemy table to filter on them 烧瓶sqlalchemy记录表中的所有更改均不起作用 - Flask sqlalchemy record all changes in the table is not working 从单独的烧瓶应用程序访问使用烧瓶 sqlalchemy 创建的数据库 - Accessing a database created with flask-sqlalchemy from a separate flask app 从烧瓶内访问mysql - accessing mysql from within flask 搜索期间访问Flask-Sqlalchemy查询中的表变量 - Accessing table variables in Flask-Sqlalchemy query during search
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM