vishavjeet

singh.vishavjeet11@gmail.com

How can export data from database using python script?

Posted on April 13, 2021



import csv

import mysql.connector

db = mysql.connector.connect( host="", user="", password="", database="")

cursor= db.cursor()

cursor.execute("SELECT p.product_title, p.product_sku, ms.size_title, ps.product_quantity QTY FROM products p INNER JOIN product_sizes ps ON p.product_id=ps.product_id INNER JOIN master_sizes ms ON ps.size_id=ms.size_id ORDER BY p.product_id DESC")

result = cursor.fetchall()

with open('product_inventory.csv', 'w', newline='') as file:

    writer = csv.writer(file)

    writer.writerow(["Product Title", "Product Sku", "Size", "QTY"])

   for x in result:

       writer.writerow(list(x))


Sign In for comment and like the post.