通过python psycopg的postgres IN子句

时间:2019-01-31 12:49:59

标签: python-3.x psycopg2

我正在尝试通过python psycopg2执行以下postgres查询

SELET * FROM users WHERE user_id IN (1,2,3,4,5);

以下是相同的python代码:

def get_usernames(userids):
    connection = psycopg2.connect("dbname=userdb user=guest host=example.com")
    connection.autocommit = True
    cursor = connection.cursor()
    cursor.execute("SELECT username FROM users WHERE userid IN {}".format(userids))
    return cursor

userids=[1,2,3,4,5]
get_usernames(userids)

我遇到以下错误:

psycopg2.ProgrammingError: syntax error at or near "["
LINE 1: ...from users WHERE userid IN {1,2...

我习惯于pymysql。这对我来说是新的。让我知道我的错误是什么。

0 个答案:

没有答案