小编use*_*185的帖子

如何在 psycopg2 选择查询中使用字符串变量?

从 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)

python psycopg2

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

psycopg2 ×1

python ×1