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

后端 未结 4 1152
醉梦人生
醉梦人生 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:41

    Try

    cursor.execute("SELECT * FROM table_name LIMIT 1")
    

    or

    cursor.execute("SELECT * FROM table_name WHERE 1=0")
    

    Both prevent massive amounts of data being rattled. The second one is perhaps more elegant. I've just checked, and even this works:

    >>>cursor.execute("SELECT LEFT(long_text,5) as short_text FROM table_name WHERE 1=0")
    >>>print cursor.description
    (('short_text', 253, 0, 5, 5, 31, 0),)
    

提交回复
热议问题