Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 how to save the data from MySQL to CSV in Flask
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:
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)
        msg = 'No Articles Found'
        return render_template('article.html', msg=msg)
Hope you can help me
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")
I have tried your code, but got error:
File "c:/Users/User/Documents/first/", line 78 while article = cur.fetchone(): ^ SyntaxError: invalid syntax
What's the problem actually?Sorry for asking
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):
        cwrtr.write(f"article[0], article[1], article[2]\n")

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  MySQL Database Flask maurosmartins 0 264 Oct-03-2019, 10:56 AM
Last Post: maurosmartins
  Using flask to add data to sqlite3 table with PRIMARY KEY catafest 1 1,869 Sep-09-2019, 07:00 AM
Last Post: buran
  how retrieve database save multiple data in web Django 2.1 taomihiranga 0 371 Jul-30-2019, 04:58 PM
Last Post: taomihiranga
  Flask data with dynamic updates from panadas parthi1705 0 325 Jun-19-2019, 09:59 AM
Last Post: parthi1705
  Read Save RadioButtons from Database in Python Flask Webpage Gary8877 0 1,513 Apr-11-2019, 12:33 AM
Last Post: Gary8877
  flask app to save images locally when deployed on heroku not working Prince_Bhatia 1 1,227 Feb-20-2019, 11:59 PM
Last Post: snippsat
  how i save the html form to flask database mebaysan 1 1,967 Feb-07-2019, 12:56 AM
Last Post: snippsat
  flask requests display data from api on webpage with javacript pascale 0 695 Oct-25-2018, 08:30 PM
Last Post: pascale
  Paginate json data in flask or jinja2 soli1994 1 3,069 Jun-28-2018, 06:00 PM
Last Post: gontajones
  How to perform predictions on a data in csv file using Flask? manjusavanth 0 1,770 May-23-2018, 09:03 AM
Last Post: manjusavanth

Forum Jump:

Users browsing this thread: 1 Guest(s)