python: MYSQLdb. how to get columns name without executing select * in a big table?

后端 未结 4 1155
醉梦人生
醉梦人生 2020-12-31 09:01

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.de         


        
4条回答
  •  梦谈多话
    2020-12-31 09:33

    You can use SHOW columns:

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

    FYI, this is essentially the same as using desc:

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

提交回复
热议问题