How can I select database with http query?

query
#1

I’m making a program which uses Influxdb and this program is made with c++.
And in my program, I use the Curl to process http query.

In my program, I’m trying to select database with http query like this,

http://localhost:8086/query?q=USE+my_database”.

But, the Influxdb returns HTTP status code, 400.
And the error message, returned as json format, is as follows:

"{\"error\":\"error parsing query: found USE, expected SELECT, DELETE, SHOW, CREATE, DROP, EXPLAIN, GRANT, REVOKE, ALTER, SET, KILL at line 1, char 1\"}\n".

I have checked if the Influxdb is working properly with “influx.exe” in the command line.
The results is shown below. As can be see, Influxdb is working well.
(the “my_database” is “tick_data”.)

Microsoft Windows [Version 10.0.17134.590]
(c) 2018 Microsoft Corporation. All rights reserved.

D:\Program Files (x86)\influxdb\1.5.2-1>influx
Connected to http://localhost:8086 version 1.5.2
InfluxDB shell version: 1.5.2
> show databases
name: databases
name
----
_internal
test_1111
tick_data
> use tick_data
Using database tick_data
>

Could anybody tell me what is problem of my http query?

#2

I don’t think you need to specify “USE DB NAME”.

curl -G 'http://localhost:8086/query?db=mydb' --data-urlencode 'q=SELECT * FROM "mymeas"'

You specify it as the value for the db part of the URL.

#3

Thank you for your reply.
Yes, your answer is enough to resolve my question.

But I just want to know why my http query does not work.
And I also want to know if it is possible to select a database with “use” command.

Using Curl program in command line in the Window 10, I have tried another http query like this,

curl -XPOST "http://localhost:8086/query" --data-urlencode "q=CREATE DATABASE "test_9999"",

and this works. I could check that the database “test_9999” was created.
After seen this query, I thought that the syntax of my previous http query could be wrong.
So I tried modified http query like this,

curl -XPOST "http://localhost:8086/query" --data-urlencode "q=USE "tick_data"".

But it also does not work as I expected.

#4

I’m not sure if “USE” is valid syntax for the http query. To be honest though I’m not 100% sure about that.

It looks as though /query?db=database_name is the equivilant of specifying USE in the query. So instead of

curl -XPOST "http://localhost:8086/query" --data-urlencode "q=USE "tick_data""

you would just specify

curl -XPOST "http://localhost:8086/query?db=database" --data-urlencode "q=SELECT * "tick_data""

In short, i don’t think you can select the database with USE through http.

Not sure if that helps, it might be worth tagging one of the forum support staff for better clarification.

#5

Thank you for your reply.

#6

use is not part of InfluxQL, it is a command provided by the CLI to set the database and retention policy:

use [ "<database_name>" | "<database_name>"."<retention policy_name>" ] Sets the current database and/or retention policy. Once influx sets the current database and/or retention policy, there is no need to specify that database and/or retention policy in queries. If you do not specify the retention policy, influx automatically queries the use d database’s DEFAULT retention policy.

In InfluxQL, the FROM clause provides several formats for specifying a measurement, including “fully qualified” measurements, which allow you to specify which database and retention policy the query should use:

FROM <database_name>.<retention_policy_name>.<measurement_name> Returns data from a fully qualified measurement. Fully qualify a measurement by specifying its database and retention policy.

Setting the database in an HTTP request using query parameters is also valid, and is documented in the /query endpoint docs under Query string parameters.

#7

Your reply is very helpful.
Thank you very much.

1 Like