从 PostgreSQL 列“u62194”获取数据时出错不存在
sql = """select userid from myusers WHERE userid='u62194' """ Of
course the hardcoded value works as it should.
import psycopg2
def select_tables():
someuserid = 'u62194'
print(someuserid)
""" Run a select using a variable that is a string """
sql = """select userid from myusers WHERE userid=%s """ %someuserid
conn = None
try:
conn = psycopg2.connect(user = "postgres",
password = "xxxxxx",
host = "127.0.0.1",
port = "5432",
database = "mydb")
cur = conn.cursor()
cur.execute(sql, (select_tables,))
print(cur.fetchone())
cur.close()
""" …Run Code Online (Sandbox Code Playgroud)