Reputation: 11
First, some context about my application:
I need to write a Python script to read the database. I want to fetch only the most recent measurement value, not all values.
Here is my code:
from influxdb import InfluxDBClient
client = InfluxDBClient(database='test')
client = InfluxDBClient()
rs = cli.query("SELECT * from CoreTemperature")
CoreTemperature = list(rs.get_points(measurement='CoreTemperature'))
print(CoreTemperature)
I am stuck because of:
**NameError: name 'cli' is not defined**
select * from CoreTemperature
will read all points saved for the measurement, but I only want the most recent value.Upvotes: 1
Views: 2663
Reputation: 11
Good morning everyone I reach the target with the following code:
>from influxdb import InfluxDBClient
>dbClient = InfluxDBClient()
>loginRecords = dbClient.query('select last(*) from CoreTemperature',
database='test')
>print(loginRecords)
Thanks for all comments and help me!
Upvotes: 0
Reputation: 11
The first error : NameError: name 'cli' is not defined
is because you defined the influxdb
client as client
, and in the query you are calling it as cli
rs = client.query("SELECT * from CoreTemperature")
this will resolve this error.
The second error will be solved by:
client.query('SELECT last(<field_name>), time FROM CoreTemperature ')
Upvotes: 1
Reputation: 7978
It sounds like you want the most recent measurement value written to CoreTemperature
. You can get that using the LAST
selector:
LAST()
Returns the field value with the most recent timestamp.
Like this:
SELECT LAST(*) FROM CoreTemperature
Upvotes: 0