0

I'm new at exporting data, I research all over the net but it was really hard for me to understand, can someone help me to know the basic about it.

This is my main problem: I want to download a specific data from mysql base on the date range I choose in my client, then when I click the download button, I want these data from mysql to be save in my computer together the user have the option to save it as CSV/Excel, I'm using python for my webservice. Thank you

This is my code right know in my webservice:

@api.route('/export_file/', methods=['GET', 'POST'])
def export_file():

    if request.method == 'POST':
        selectAttendance = """SELECT * FROM attendance"""
        db.session.execute(selectAttendance)
        db.session.commit()


        f = csv.writer(open("file.csv", "w"))
        for row in selectAttendance:
            f.writerow([str(row)])





    return jsonify({'success': True})
2
  • You should tell us which web framework you are using. I assume this is flask. Commented Nov 7, 2014 at 9:04
  • Sorry I forget to include it, yes I'm I'm using flask, I edit my heading now Commented Nov 7, 2014 at 9:08

2 Answers 2

2

In general:

  1. Set the mime header "Content-Type" part of the http header to the corresponding MIME-Type matching your data. This tells the browser what type of data the webserver is going to send.
  2. Send the actual data in the 'body'

With flask:

Forcing application/json MIME type in a view (Flask)

http://flask.pocoo.org/docs/0.10/patterns/streaming/

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

1 Comment

Hi thank you for this. It helps me a lot especially with the content header. I can save now my data. Thanks again.
1
def get(self):
    try:
        os.stat(BACKUP_PATH)
    except:
        os.mkdir(BACKUP_PATH)
    now = datetime.now() # current date and time
    year = now.strftime("%Y")
    month = now.strftime("%m")
    day = now.strftime("%d")
    time = now.strftime("%H:%M:%S")
    date_time = now.strftime("%d_%m_%Y_%H:%M:%S")
    TODAYBACKUPPATH = BACKUP_PATH + '/' + date_time

    try:
        os.stat(TODAYBACKUPPATH)
    except:
        os.mkdir(TODAYBACKUPPATH)
    print ("checking for databases names file.")

    if os.path.exists(DB_NAME):
        file1 = open(DB_NAME)
        multi = 1
        print ("Databases file found...")
        print ("Starting backup of all dbs listed in file " + DB_NAME)
    else:
        print ("Databases file not found...")
        print ("Starting backup of database " + DB_NAME)
        multi = 0

    if multi:
        in_file = open(DB_NAME,"r")
        flength = len(in_file.readlines())
        in_file.close()
        p = 1
        dbfile = open(DB_NAME,"r")

        while p <= flength:
            db = dbfile.readline()   # reading database name from file
            db = db[:-1]         # deletes extra line
            dumpcmd = "mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
            os.system(dumpcmd)
            gzipcmd = "gzip " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
            os.system(gzipcmd)
            p = p + 1
        dbfile.close()
    else:
        db = DB_NAME
        dumpcmd = "mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
        os.system(dumpcmd)
        gzipcmd = "gzip " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
        os.system(gzipcmd)
        # t = ("Your backups have been created in '" + TODAYBACKUPPATH + "' directory")
        return "Your Folder have been created in '" + TODAYBACKUPPATH + "'." 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.