Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to create a Database and query whether it exits or not #3

Closed
caspianron opened this issue Mar 13, 2018 · 2 comments
Closed

Comments

@caspianron
Copy link

Hi,
Thank you very much for your for solving my previous issue. I have a question 👍
Which function to call from .hpp file to ""create a Database and query whether it exits or not"".?

Thanks
Raunak

@orca-zhang
Copy link
Owner

orca-zhang commented Mar 13, 2018

Hi,

Good day.

I've added a method influxdb_cpp::create_db to support creating a database according to the offical document.

You can refer to demo file for more details.

And query whether it exists or not, the best practice I think is below:

    string resp;
    influxdb_cpp::server_info si("127.0.0.1", 8086, "", "test" /* usr */, "test" /* passwd */);
    int ret = influxdb_cpp::query(resp, "show databases", si);
    // check error code
    // assert(ret == 0);

Then check the db list in the resp, the json response is like below.

{
	"results": [{
		"series": [{
			"name": "databases",
			"columns": ["name"],
			"values": [
				["_internal"],
				["test"],
				["test1"],
				["x"]
			]
		}]
	}]
}

Using xpjson:

#include <xpjson.hpp>

bool check_db_exists(const string& resp, const string& db_name)
    JSON::Value v;
    v.read(resp);
    JSON::Array& a = v["results"][0]["series"][0]["values"].a();
    for(size_t i = 0; i < a.size(); ++i) {
        if(a[i][0] == db_name) {
            return true;
        }
    }
    return false;
}

You should only check the error code of influxdb::query, don't worry if the response is empty or error occurred, the result is always OK.

Thank you for your feedback.

Orca

@caspianron
Copy link
Author

Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants