从 python 中 table 的数据集中获取列名称
Get column name from a table's dataset in python
我正在尝试从 table
中获取列
query = session.prepare(""" SELECT * FROM mytable """)
row_data = session.execute(query, )
我想要的是从row_data中获取列名。
有办法吗?
在大多数 python 数据库适配器中,您可以使用 DictCursor
通过类似于 Python 字典而不是元组的接口来检索记录。
使用卡桑德拉:
>>> from cassandra.query import dict_factory
>>> session = cluster.connect('mykeyspace')
>>> session.row_factory = dict_factory
>>> rows = session.execute("SELECT name, age FROM users LIMIT 1")
>>> print rows[0]
{u'age': 42, u'name': u'Bob'}
使用 psycopg2:
>>> dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
>>> dict_cur.execute("INSERT INTO test (num, data) VALUES(%s, %s)",
... (100, "abc'def"))
>>> dict_cur.execute("SELECT * FROM test")
>>> rec = dict_cur.fetchone()
>>> rec['id']
1
>>> rec['num']
100
>>> rec['data']
"abc'def"
使用 MySQLdb:
>>> import MySQLdb
>>> import MySQLdb.cursors
>>> myDb = MySQLdb.connect(user='andy47', passwd='password', db='db_name', cursorclass=MySQLdb.cursors.DictCursor)
>>> myCurs = myDb.cursor()
>>> myCurs.execute("SELECT columna, columnb FROM tablea")
>>> firstRow = myCurs.fetchone()
{'columna':'first value', 'columnb':'second value'}
你可以这样,
fields = [ix[0] for ix in cursor.description]
cursor.description # None initially, list of N tuples that represent
the N columns in a row after an execute. Only
contains type and name info, not values.
使用 cassandra-driver 3.14,您还可以通过访问查询结果条目的 _fields 参数来获取列名。
from cassandra.cluster import Cluster
session = Cluster().connect('mykeyspace')
rows = session.execute('select * from mytable limit 1')
column_names = rows.one()._fields
我正在尝试从 table
中获取列 query = session.prepare(""" SELECT * FROM mytable """)
row_data = session.execute(query, )
我想要的是从row_data中获取列名。 有办法吗?
在大多数 python 数据库适配器中,您可以使用 DictCursor
通过类似于 Python 字典而不是元组的接口来检索记录。
使用卡桑德拉:
>>> from cassandra.query import dict_factory
>>> session = cluster.connect('mykeyspace')
>>> session.row_factory = dict_factory
>>> rows = session.execute("SELECT name, age FROM users LIMIT 1")
>>> print rows[0]
{u'age': 42, u'name': u'Bob'}
使用 psycopg2:
>>> dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
>>> dict_cur.execute("INSERT INTO test (num, data) VALUES(%s, %s)",
... (100, "abc'def"))
>>> dict_cur.execute("SELECT * FROM test")
>>> rec = dict_cur.fetchone()
>>> rec['id']
1
>>> rec['num']
100
>>> rec['data']
"abc'def"
使用 MySQLdb:
>>> import MySQLdb
>>> import MySQLdb.cursors
>>> myDb = MySQLdb.connect(user='andy47', passwd='password', db='db_name', cursorclass=MySQLdb.cursors.DictCursor)
>>> myCurs = myDb.cursor()
>>> myCurs.execute("SELECT columna, columnb FROM tablea")
>>> firstRow = myCurs.fetchone()
{'columna':'first value', 'columnb':'second value'}
你可以这样,
fields = [ix[0] for ix in cursor.description]
cursor.description # None initially, list of N tuples that represent the N columns in a row after an execute. Only contains type and name info, not values.
使用 cassandra-driver 3.14,您还可以通过访问查询结果条目的 _fields 参数来获取列名。
from cassandra.cluster import Cluster
session = Cluster().connect('mykeyspace')
rows = session.execute('select * from mytable limit 1')
column_names = rows.one()._fields