How to get table column-name/header for SQL query in python

拈花ヽ惹草 提交于 2020-12-31 14:06:17

问题


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


回答1:


In DB-API 2.0 compliant clients, cursor.description is a sequence of 7-item sequences of the form (<name>, <type_code>, <display_size>, <internal_size>, <precision>, <scale>, <null_ok>), 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.




回答2:


Try Pandas .read_sql(), I can't check it right now but it should be something like:

 pd.read_sql( Q , connection)



回答3:


my english is poor i write a sample, see it ,use cx_Oracle, but mysql the same it .

import cx_Oracle


def test_oracle():
    connection = cx_Oracle.connect('user', 'password', 'tns')
    try:
        cursor = connection.cursor()
        cursor.execute('SELECT day_no,area_code ,start_date from dic.b_td_m_area where rownum<10')

        #only print head
        title = [i[0] for i in cursor.description]
        print(title)

        # column info
        for x in cursor.description:
            print(x)

    finally:
        cursor.close()


if __name__ == "__main__":
    test_oracle();



回答4:


Step 1: Select your engine like pyodbc, SQLAlchemy etc.

Step 2: Establish connection cursor = connection.cursor()

Step 3: Execute SQL statement cursor.execute("Select * from db.table where condition=1")

Step 4: Extract Header from connection variable description

headers = [i[0] for i in cursor.description]
print(headers)


来源:https://stackoverflow.com/questions/34214031/how-to-get-table-column-name-header-for-sql-query-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!