Python Forum
how to save the data from MySQL to CSV in Flask
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to save the data from MySQL to CSV in Flask
#1
Hi..I want to save the data from MySQL to CSV in Flask. The data that need to be saved, consists of id,q1 and q2. The problem is, it does not generate all the data. Instead, it just shown id,q1 and q2 only in the csv, as shown example below:

-------------------
id q1 q2
------------------
id q1 q2
-------------------


The code are below:
@app.route('/article/<string:id>/')
def article(id):
# Create cursor
    cur = mysql.connection.cursor()

    # Get article
    result = cur.execute("SELECT id,q1,q2 FROM table WHERE id = %s", [id])

    article = cur.fetchall()

    with open('database.csv', 'w', newline= '') as f:
            a = csv.writer(f, delimiter=',')
            a.writerows(article)  ## closing paren added

    if result > 0:
        return render_template('article.html', article=article)
    else:
        msg = 'No Articles Found'
        return render_template('article.html', msg=msg)
Hope you can help me
Reply


Messages In This Thread
how to save the data from MySQL to CSV in Flask - by farah97 - Dec-30-2019, 03:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Save JSON data to sqlite database on Django Quin 0 2,890 Mar-26-2022, 06:22 PM
Last Post: Quin
  How can users store data temporarily in flask app? darktitan 6 2,977 Mar-21-2022, 06:38 PM
Last Post: darktitan
  trying to save data automatically from this page thunderspeed 1 2,026 Sep-19-2021, 04:57 AM
Last Post: ndc85430
  Flask Can't Save Screenshot to Postgres Db firebird 3 2,396 Sep-21-2020, 09:22 PM
Last Post: firebird
  MySQL Database Flask maurosmartins 0 1,826 Oct-03-2019, 10:56 AM
Last Post: maurosmartins
  Using flask to add data to sqlite3 table with PRIMARY KEY catafest 1 3,753 Sep-09-2019, 07:00 AM
Last Post: buran
  how retrieve database save multiple data in web Django 2.1 taomihiranga 0 2,818 Jul-30-2019, 04:58 PM
Last Post: taomihiranga
  Flask data with dynamic updates from panadas parthi1705 0 2,117 Jun-19-2019, 09:59 AM
Last Post: parthi1705
  Read Save RadioButtons from Database in Python Flask Webpage Gary8877 0 7,182 Apr-11-2019, 12:33 AM
Last Post: Gary8877
  flask app to save images locally when deployed on heroku not working Prince_Bhatia 1 5,283 Feb-20-2019, 11:59 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020