I am new to python. I have the following code where I need to call the same variable multiple times. How can I do this?
import psycopg2 as pg
conn = pg.connect("dbname='db' user='user' host='localhost'")
q1 = """SELECT value FROM t1;"""
cur1 = conn.cursor()
cur1.execute(q1)
cur2 = conn.cursor()
for row in cur1:
q2 = """SELECT * FROM t2 WHERE c1 = '%s' or c2 ='%s';"""
#what comes here? %s is the 'value' from q1
cur2.execute(q2)
for row in cur2:
print(row)
How can I tell python to use 'value' for all appearances of '%s'? %(row) works when %s appears only once:
q2 = """SELECT * FROM t2 WHERE c1 = '%s' or c2 ='%s';"""(%row)
I searched the stackoverflow but could not find my answer. I hope it is not a duplicate question. Thank you.