我在python方面很菜鸟,而且我正在尝试在某些表中进行一些操作。 目前,我正在对表进行硬编码:
cur.execute("select id,email from table1;")
但是我想将表更改为和变量以构造一个函数,例如:
cur.execute("select id,email from table;")
我该怎么做?
非常感谢
我将python3
与psycopg2
一起使用
#open connection with db
conn = psycopg2.connect(host="localhost",database="test", user="test", password="test")
cur = conn.cursor()
#Select id
cur.execute("select id,email from customers;")
#put ids on list
row = cur.fetchone()
我也尝试过:
sql = "select id,email from %s"
val = customers
cur.execute(sql, val)
我有这个错误:
File "updateemails.py", line 14, in <module>
val = (customers)
NameError: name 'customers' is not defined```