7

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

4 Answers 4

19

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.

Sign up to request clarification or add additional context in comments.

2 Comments

brilliant! Just an example in case someone is trying to load an sql query into a pandas dataframe respecting column names from query: cur.execute(q) import pandas as pd pd.DataFrame.from_records(cur, columns=[i[0] for i in cur.description])
Save my day! Tks!
9

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)

1 Comment

and to print out the content of rows use : for row in cursor: print(row)
5

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

 pd.read_sql( Q , connection)

Comments

5

Here is a sample code using cx_Oracle, that should do what is expected:

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.