0

Below is my code, but this is not writing anything in file

import os
import pymysql
import pandas as pd
import csv

host = os.getenv('MYSQL_HOST')
port = os.getenv('MYSQL_PORT')
user = os.getenv('MYSQL_USER')
password = os.getenv('MYSQL_PASSWORD')
database = os.getenv('MYSQL_DATABASE')

conn = pymysql.connect(
    host=host,
    port=int(3306),
    user="root",
    passwd="Pass200",
    db="test_db",
    charset='utf8mb4')

QUERY='SELECT * FROM act;'

df = pd.read_sql_query("SELECT * FROM act",conn)
df.tail(10)

Above code successfully print SQL DB data. I appended below code to above code for writing all data alongwith column names in EXCEL Sheet:

   cur=conn.cursor()
   cur.execute(QUERY)
   result=cur.fetchall()
   c = csv.writer(open('test.csv', 'w'))
   for x in result:
       print(x);
       c.writerow(x)

Kindly assist as I am new to this.

1 Answer 1

1

Since you are using pandas you can use df.to_csv('test.csv')

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.