0

I am trying to get the column names to print for my code below, it only prints the results of the queries, the results being underneath each other.

Is there a way to append the column names above each query result? Or is there a way to append custom column names about each result?

cur = db.cursor()
cur = db2.cursor()
cur = db3.cursor()

#SQL Queries

dbQuery="SELECT COUNT(DISTINCT Name) FROM blah;"

dbQuery2="SELECT COUNT(Pause) FROM blah WHERE Pause = 1;"

dbQuery3="SELECT COUNT(Pause) FROM blah WHERE Pause = 0;"

result = []

cur.execute(dbQuery)

result.extend(cur.fetchall())

cur.execute(dbQuery2)

result.extend(cur.fetchall())

cur.execute(dbQuery3)

result.extend(cur.fetchall())

column_names = [i[0] for i in cur.description]
fp = open('Result_Set.csv', 'w')
myFile = csv.writer(fp)
myFile.writerow(column_names) 
myFile.writerows(result)
fp.close()

I feel as though I am missing something simple.

Any help would be greatly appreciated

Thanks

user3296793
  • 270
  • 2
  • 14
  • Maybe this could help: https://stackoverflow.com/questions/19482970/get-list-from-pandas-dataframe-column-headers – Tony Dec 18 '18 at 17:08
  • You don't have any column names in your sql. select count(distinct names) as mynames from mytable. – danny117 Dec 18 '18 at 18:12

0 Answers0