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,field2measurementnodeandname110
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
conflook like, can you add thatconfdicttagsalways just a single key-value pair?""", since you both have single and double quotes in there, is that correct? also why do you have"node"='name1'and notnode=name1?