python:MYSQLdb.如何在不执行大表中的select *的情况下获取列名? [英] python: MYSQLdb. how to get columns name without executing select * in a big table?

查看:104
本文介绍了python:MYSQLdb.如何在不执行大表中的select *的情况下获取列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个表的列名,但是其中有超过一百万个数据. 所以我不能使用:

I want to get the column names of a table, but there a over million data in it. So I cannot use:

cursor.execute("SELECT * FROM table_name")
print cursor.description

在sqlite3中,我是这样做的

And in sqlite3, I do it this way

crs.execute("PRAGMA table_info(%s)" %(tablename[0]))
for info in crs:
    print info

但是这在python mysqldb中不起作用.有人知道怎么做吗?

But this is not working in python mysqldb. Any one know how to do that?

推荐答案

您可以使用 SHOW columns :

You can use SHOW columns:

cursor.execute("SHOW columns FROM table_name")
print [column[0] for column in cursor.fetchall()]

仅供参考,这与使用 desc :

FYI, this is essentially the same as using desc:

cursor.execute("desc table_name")
print [column[0] for column in cursor.fetchall()]

这篇关于python:MYSQLdb.如何在不执行大表中的select *的情况下获取列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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