Jun-25-2018, 02:11 PM
@upload_csv_blueprint.route('/upload_csv', methods=['GET','POST'])
def upload_file():
if request.method == 'POST':
csvfile = request.files['file']
reader = csv.DictReader(csvfile)
data = [row for row in reader]
for row in data:
date_time_store = row['date_time']
technician_store = row['technician']
test_location_store = row['test_location']
product_serial_number_store = row['product_serial_number']
test_detail_store = row['test_detail']
test_result_store = row['test_result']
query = test_result(date_time = date_time_store,
technician_name = technician_store,
place_of_test = test_location_store,
serial_number=product_serial_number_store,
test_details=test_detail_store,
result=test_result_store)
db.session.add(query)
db.session.commit()
return('Did it work?')
else:
return redirect(url_for('upload_csv.upload_csv_layout'))#The code above is my current version. This version uploads the second line of the csv file ( the first line being the headings used to match up the columns.)The csv files i will be uploading could contain up too 1000 lines of data. I was hoping the method I had constructed would loop and add all 1000 entries. But it appears to just quit after the first.
I am constructing my project on a Flask platform, using sql alchemy. If anyone knows where my error lies or knows of an easy way to upload a csv file into a sql database. Would be really appreciated as I have spent the better part of a 2 days searching for the answer.
