0

I have made a small flask app that pulls data from a MySQL source and places it into a table, however, the MySQL data is not correctly populating the table. (edit - by this i mean it is not populating at all, my bad)

mydb = mysql.connector.connect(
    host=x,
    user=x,
    passwd=x,
    database=x
)
def browserSettings_mysql():
    curs = mydb.cursor()
    curs.execute("SELECT DATE_FORMAT(dtime, '%Y-%m-%d %T') as timest, moisture, temp FROM moisture")
    return curs.fetchall()
@app.route('/')
def index():
       return render_template("index.html", rows = moistureControl.browserSettings_mysql())
  <table>
    <tr>
      <th>Time</th>
      <th>Temp</th>
      <th>Moisture</th>
    </tr>
    {% for row in rows %}
      <tr>
        <td>{{row.timest}}</td>
        <td>{{row.temp}}</td>
        <td>{{row.moisture}}</td>
      </tr>
    {% endfor %}
  </table>

How can I solve this?

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Paul
  • 25
  • 3
  • Have you confirmed that `moistureControl.browserSettings_mysql()` is actually returning the data you expect? If so what does that data look like? – noslenkwah Apr 28 '20 at 14:44
  • @noslenkwah It came out as ```[('2020-04-28 06:45:51', 2.0, 2.0), ('2020-04-28 07:10:22', 90.0, 22.0)]``` when printed to the console. I'm not sure if the row titles should be included in the print or not, that might be causing it not to work though – Paul Apr 28 '20 at 14:50
  • @noslenkwah I created a work around by just calling the rows by their index (row[0], row[1], etc) but is there a way to output the mysql data with row titles? – Paul Apr 28 '20 at 14:55

1 Answers1

1

You rows object is a list of tuples. You will have to access the data by index (e.g. row[0] instead of row.timest).

  <table>
    <tr>
      <th>Time</th>
      <th>Temp</th>
      <th>Moisture</th>
    </tr>
    {% for row in rows %}
      <tr>
        <td>{{row[0]}}</td>
        <td>{{row[2]}}</td>
        <td>{{row[1]}}</td>
      </tr>
    {% endfor %}
  </table>
noslenkwah
  • 1,702
  • 1
  • 17
  • 26