Search code examples
pythoninfluxdbinfluxdb-python

Query Influx database


I have this code to query a influx DB, but it is not working at all. Here is the python code.

import os

from influxdb import InfluxDBClient

username = u'{}'.format(os.environ['INFLUXDB_USERNAME'])
password = u'{}'.format(os.environ['INFLUXDB_PASSWORD'])

client = InfluxDBClient(host='127.0.0.1', port=8086, database='data',
                        username=username, password=password)

result = client.query("SELECT P_askbid_midprice1 FROM 'DCIX_OB' WHERE time > '2018-01-01';")

I got the following error, but it's still unclear how to fix the code above. If I query directly from influxdb with bash with SELECT P_askbid_midprice1 FROM 'DCIX_OB' WHERE time > '2018-01-01'; it worked perfectly fine.

    Press ENTER or type command to continue
    Traceback (most recent call last):
      File "graph_influxdb.py", line 11, in <module>
        result = client.query("SELECT P_askbid_midprice1 FROM 'DCIX_OB' WHERE time > '2018-01-01';")


  File "/home/ubuntu/.local/lib/python3.5/site-packages/influxdb/client.py", line 394, in query
    expected_response_code=expected_response_code
  File "/home/ubuntu/.local/lib/python3.5/site-packages/influxdb/client.py", line 271, in request
    raise InfluxDBClientError(response.content, response.status_code)
influxdb.exceptions.InfluxDBClientError: 400: {"error":"error parsing query: found DCIX_OB, expected identifier at line 1, char 31"}

How can I fix it?


Solution

  • result = client.query("SELECT P_askbid_midprice1 FROM DCIX_OB WHERE time > '2018-01-01'")

    this should work