python怎么获取数据库表项的名字,如何在python中获取用于SQL查询的表列名称/标题...

I have the data in pandas dataframe which I am storing in SQLITE database using Python. When I am trying to query the tables inside it, I am able to get the results but without the column names. Can someone please guide me.

sql_query = """Select date(report_date), insertion_order_id, sum(impressions), sum(clicks), (sum(clicks)+0.0)/sum(impressions)*100 as CTR

from RawDailySummaries

Group By report_date, insertion_order_id

Having report_date like '2014-08-12%' """

cursor.execute(sql_query)

query1 = cursor.fetchall()

for i in query1:

print i

Below is the output that I get

(u'2014-08-12', 10187, 2024, 8, 0.3952569169960474)

(u'2014-08-12', 12419, 15054, 176, 1.1691244851866613)

What do I need to do to display the results in a tabular form with column names

解决方案

In DB-API 2.0 compliant clients, cursor.description is a sequence of 7-item sequences of the form (, , , , , , ), one for each column, as described here. Note description will be None if the result of the execute statement is empty.

If you want to create a list of the column names, you can use list comprehension like this: column_names = [i[0] for i in cursor.description] then do with them whatever you'd like.

Alternatively, you can set the row_factory parameter of the connection object to something that provides column names with the results. An example of a dictionary-based row factory for SQLite is found here, and you can see a discussion of the sqlite3.Row type below that.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值