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
#2
In reality, you don't even need csv.writer as the structure is so simple, but if used:
remove line 9, instead use something like (untested):
with open('database.csv', 'w', newline= '') as f:
    cwrtr = csv.writer(f, delimiter=',')
    while article = cur.fetchone():
        cwrtr.write(f"article[0], article[1], article[2]\n")
Reply
#3
I have tried your code, but got error:
Error:
File "c:/Users/User/Documents/first/app.py", line 78 while article = cur.fetchone(): ^ SyntaxError: invalid syntax
What's the problem actually?Sorry for asking
Reply
#4
I did say it was untested.
I guess it has to be done a bit differently:
with open('database.csv', 'w', newline= '') as f:
    cwrtr = csv.writer(f, delimiter=',')
    while True:
        article = cur.fetchone()
        if len(article == 0):
            break
        cwrtr.write(f"article[0], article[1], article[2]\n")
Reply
#5
thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Save JSON data to sqlite database on Django Quin 0 2,804 Mar-26-2022, 06:22 PM
Last Post: Quin
  How can users store data temporarily in flask app? darktitan 6 2,858 Mar-21-2022, 06:38 PM
Last Post: darktitan
  trying to save data automatically from this page thunderspeed 1 1,970 Sep-19-2021, 04:57 AM
Last Post: ndc85430
  Flask Can't Save Screenshot to Postgres Db firebird 3 2,341 Sep-21-2020, 09:22 PM
Last Post: firebird
  MySQL Database Flask maurosmartins 0 1,787 Oct-03-2019, 10:56 AM
Last Post: maurosmartins
  Using flask to add data to sqlite3 table with PRIMARY KEY catafest 1 3,702 Sep-09-2019, 07:00 AM
Last Post: buran
  how retrieve database save multiple data in web Django 2.1 taomihiranga 0 2,763 Jul-30-2019, 04:58 PM
Last Post: taomihiranga
  Flask data with dynamic updates from panadas parthi1705 0 2,063 Jun-19-2019, 09:59 AM
Last Post: parthi1705
  Read Save RadioButtons from Database in Python Flask Webpage Gary8877 0 7,113 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,229 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