'How to use Python-Operator % for multiple values?
I'm trying to fill an SQL database with soma data, measured by an bme680. Therefore I'd like to format the measured values like this:
print ("Temperatur: %0.1f C" % (bme680.temperature + temperature_offset))
print ("Gas: %d ohm" % bme680.gas)
print ("Luftfeuchtigkeit: %0.1f %%" % bme680.relative_humidity)
print ("Luftdruck: %0.3f hPa" % bme680.pressure)
To fill my table I use this command:
curs.execute("INSERT INTO bme680_tb (datum, uhrzeit, temperatur, gas, luftfeuchtigkeit, luftdruck) VALUES (CURRENT_DATE(), NOW(), %0.1f, 999.99, 999.99, 999.99);"%bme680.temperature)
But instead of 999.99 I would like to use more data from bme680 formatted with the %-operator.
How can I do that? Thanks for your help.
Solution 1:[1]
I found a quite simple solution: Split up the string like this:
string = "INSERT INTO bme680_tb (datum, uhrzeit, temperatur, gas, luftfeuchtigkeit, luftdruck) "
string += "VALUES (CURRENT_DATE(), NOW(), %0.1f, " %bme680.temperature
string += "%d, " %bme680.gas
string += "%0.1f, " %bme680.relative_humidity
string += "%0.3f" %bme680.pressure
string += ");"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rachel Gallen |
