本文介绍了python sqlalchemy 动态获取列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
connection = create_engine('mysql://user:passwd@localhost:3306/db').connect()
result = connection.execute("select * from table")
for v in result:
print v['id']
print v['name']
connection.close()
如何动态获取表格列名称?在这种情况下 id
和 name
how i can get TABLES COLUMNS NAMES dynamically? in this case id
and name
推荐答案
您可以通过调用 result.keys()
来查找列,也可以通过调用 v.keys 来访问它们()
在 for
循环中.
You can either find the columns by calling result.keys()
or you can access them through calling v.keys()
inside the for
loop.
这是一个使用 items()
的例子:
Here's an example using items()
:
for v in result:
for column, value in v.items():
print('{0}: {1}'.format(column, value))
这篇关于python sqlalchemy 动态获取列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!