Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1251

Python • Re: RRDtool & python3 code optimization

$
0
0
This code gets hometemp and temp in one python script. twotemps.py:

Code:

#!/usr/bin/python3import mysql.connectormydb = mysql.connector.connect(  host="localhost",  database="exampledb",  user="pi",  passwd="password")mycursor = mydb.cursor()sql = "select hometemp,temp from outsidetemp where id = (select max(id)-1 from outsidetemp)"mycursor.execute(sql)data = mycursor.fetchone()hometemp = data[0]temp = data[1]mycursor.close()mydb.close()print(hometemp,temp)
My test table looks like this

Code:

MariaDB [exampledb]> select * from outsidetemp;+----+----------+-------+| id | hometemp | temp  |+----+----------+-------+|  1 |    21.00 | 31.20 ||  2 |    22.00 | 32.20 ||  3 |    23.00 | 33.20 ||  4 |    24.00 | 34.20 ||  5 |    25.00 | 35.20 ||  6 |    26.00 | 36.20 |+----+----------+-------+

Statistics: Posted by neilgl — Sat Aug 10, 2024 12:37 pm



Viewing all articles
Browse latest Browse all 1251

Trending Articles