简体   繁体   中英

string creating for jsonb query using psycopg2

I have following query with works in db

SELECT count(*) as count
FROM data
WHERE name ='user' AND
      dt > date_trunc('month', current_timestamp) AND 
      submited_jsonb @> '{"Type":["New"]}'
GROUP BY mage, date_trunc('day', dt)

Now following in my python version where I have to pass just name

query = """SELECT count(*) as count
           FROM data
           WHERE name ='{0}' AND
                 dt > date_trunc('month', current_timestamp) AND 
                 submited_jsonb @> '{"Type":["New"]}'
           GROUP BY mage, date_trunc('day', dt)""".format(user)

However above string thowing following error

ERROR:root:An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line string', (1, 12))

I have reduce error to following line '{"Type":["New"]}' in string.

What should I do ?

You're using "...".format() , so you should double the literal {} to escape them.

...
submited_jsonb @> '{{"Type":["New"]}}'
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM