1

I wish to format a string necessary for querying a database as follows:

SELECT "field1", "field2" FROM "measurement" where "status"=0 AND "node"='name1' LIMIT 10

the following comes from a dict:

  • field1, field2
  • measurement
  • node and name1
  • 10

The dict is as follows:

conf = {
  'fields': ['field1', 'field2'],
  'measurement': 'measurement',
  'limit': 10,
  'tags': {'node': 'name1'}
}

I am able to format the string a part of the string (without the tags) as follows:

    QUERY = 'SELECT "{}" FROM {} WHERE "status"=0 LIMIT {}'.format(
             '","'.join(conf['fields'],
             conf['measurement'],
             conf['limit'])

This provides me:

SELECT "field1", "field2" FROM measurement WHERE "status"=0 LIMIT 10

Problem

The key value pair within the tags is in many case dynamic i.e. there is no previously known key (in this example node)

For the string:

QUERY = 'SELECT {} FROM {} WHERE "status"=0 AND "{}"=\'{}\' LIMIT {}

I wish to fill the string format AND "{}"=\'{}\' dynamically (without prior knowledge of the key value pair within tags

How do I achieve this?

I am not sure if:

QUERY = 'SELECT "{}" FROM {} WHERE "status"=0 AND "{}"=\'{}\' LIMIT {}'.format(
                '","'.join(conf['fields']),
                conf['measurement'],
                **conf['tags'],
                conf['limit']
            )

works as it throws the following SyntaxError:

SyntaxError: positional argument follows keyword argument unpacking
8
  • How does your conf look like, can you add that Commented Jul 21, 2019 at 13:05
  • it is already added: see the conf dict Commented Jul 21, 2019 at 13:06
  • @Shan-Desai is tags always just a single key-value pair? Commented Jul 21, 2019 at 13:10
  • 1
    @Adam.Er8 for the initial case yes. Commented Jul 21, 2019 at 13:11
  • The first string you have can only be represented in python if we use """ , since you both have single and double quotes in there, is that correct? also why do you have "node"='name1' and not node=name1 ? Commented Jul 21, 2019 at 13:14

2 Answers 2

1

I have created the formatted string using the dictionary conf['tags'] beforehand to make it easier to apply to QUERY

conf = {
  'fields': ['field1', 'field2'],
  'measurement': 'measurement',
  'limit': 10,
  'tags': {'node': 'name1'}
}

#Create the string using dictionary
dict_str = ''.join('"{}"=\'{}\''.format(key,value) for key, value in conf['tags'].items())

#Create the final format string
QUERY = 'SELECT "{}" FROM "{}" WHERE "status"=0 AND {} LIMIT {}'.format(
                '","'.join(conf['fields']),
                conf['measurement'],
                dict_str,
                conf['limit']
            )
print(QUERY)

The output will be

SELECT "field1","field2" FROM "measurement" WHERE "status"=0 AND "node"='name1' LIMIT 10
Sign up to request clarification or add additional context in comments.

1 Comment

works for me! I had a look into the Client Library there might be something in the lines where one makes a generic query of everything and then parse it using the tags dict but your solution is cleaner and easy to work with.
0

If you are using python 3.6 and above there is a new way of string formating using f-strings as specified here (python docs). I am not sure I understood your request but that's what I thought you meant:

key = list(conf['tags'].keys())[0]

QUERY = f"""SELECT {conf['fields'][0]}, {conf['fields'][1]} FROM "{conf['measurement']}" WHERE "status"=0 AND "{key}"='{conf['tags'][key]}' LIMIT {conf['limit']}"""

That results in this string:

SELECT "field1", "field2" FROM "measurement" WHERE "status"=0 AND "node"='name1' LIMIT 10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.