I trying to extract data from mysql to a csv-file. The problem is the rows dont comes i same order as in mysql.
In mysql I have this in uid table 1,2,3,4,5,6,7,8,9,10,11 but the response is 10,9,11,2,1,4,3,6,5,8,7.
cursor=conn.cursor()
cursor.execute("SELECT * FROM test ORDER BY uid;")
rows = cursor.fetchall()
#rows = sorted(rows1)
allResults = {}
for row in rows:
#for row in rows:
uid = str(row[0])
data = str(row[1])
allResults[uid] = [uid,data]
#numbers.append(allResults)
return allResults
def getCSV():
if request.method == 'POST':
nr = str(request.form['nrsearch']).strip()
try = str(request.form['trysearch']).strip()
year = str(request.form['yearsearch']).strip()
allResults = readFromDBCSV(nr, try, year)
#print(allResults)
filename = str(nr)+"."+str(try)+"."+str(year)+'.csv'
if len(allResults) > 0:
with open('static/CSV/'+str(filename), 'wb') as csvfile:
CSVwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
CSVwriter.writerow(['test'])
for result in allResults:
CSVwriter.writerow(allResults[result])
return '''DONE '''+str(len(allResults))+''' RESULTS<br>
<form method=post enctype=multipart/form-data action="/doneCSV/">
<input type="hidden" name="filename" value="'''+str(filename)+'''">
<input type=submit name="Download CSV" value="Download CSV"><br>
<a href="/getCSV/">Return</a>'''
else:
return 'NO RESULTS<br><a href="/getCSV/">Return</a>'
return render_template('searchCSV.html')