如何在python中的sqlsoup中获取模式的列名? [英] How to get column names of a schema in sqlsoup in python?

查看:24
本文介绍了如何在python中的sqlsoup中获取模式的列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Python 中使用 SQLSOUP 从架构/表中获取字典中的列名和类型?使用 MySQLDB,我可以创建一个游标并使用 cursor.description.sqlsoup 有什么等价物吗?

解决方案

根据 文档:

<块引用>

可以指示 Table 对象从对应的数据库架构对象已经存在于数据库.这个过程称为反射.在最简单的情况下您只需要指定表名、一个 MetaData 对象和自动加载=真标志.如果 MetaData 没有被持久绑定,也添加 autoload_with 参数:

<预><代码>>>>消息 = 表('消息',元,自动加载=真,autoload_with=引擎)>>>[messages.columns 中 c 的 c.name]['message_id', 'message_name', 'date']

How do you get column names and types in a dictionary from a schema/table using SQLSOUP in python? With MySQLDB I can create a cursor and use cursor.description. is there some equivalent for sqlsoup?

解决方案

According to the documentation:

A Table object can be instructed to load information about itself from the corresponding database schema object already existing within the database. This process is called reflection. In the most simple case you need only specify the table name, a MetaData object, and the autoload=True flag. If the MetaData is not persistently bound, also add the autoload_with argument:

>>> messages = Table('messages', meta, autoload=True, autoload_with=engine)
>>> [c.name for c in messages.columns]
['message_id', 'message_name', 'date']

这篇关于如何在python中的sqlsoup中获取模式的列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆