Passing list of parameters to SQL in psycopg2

后端 未结 3 515
时光取名叫无心
时光取名叫无心 2020-12-24 10:51

I have a list of ids of rows to fetch from database. I\'m using python and psycopg2, and my problem is how to effectively pass those ids to SQL? I mean that if I know the le

相关标签:
3条回答
  • 2020-12-24 11:13

    Now sql module of psycopg2 (https://www.psycopg.org/docs/sql.html) can be used to safeguard against errors and injections, like e.g.:

    import psycopg2
    from psycopg2 import sql
    
    params = config()
    conn = psycopg2.connect(**params)
    cur = conn.cursor()
    
    ids = ['a','b','c']
    sql_query = sql.SQL('SELECT * FROM {} WHERE id IN ({});').format(
                        sql.Identifier('table_name'),
                        sql.SQL(',').join(map(sql.Identifier, ids))
                    )
    print (sql_query.as_string(cur)) # for debug
    cur.execute(sql_query)
    
    from configparser import ConfigParser
    def config(filename='database.ini', section='postgresql'):
        # create a parser
        parser = ConfigParser()
        # read config file
        parser.read(filename)
    
        # get section, default to postgresql
        db = {}
        if parser.has_section(section):
            params = parser.items(section)
            for param in params:
                db[param[0]] = param[1]
        else:
            raise Exception('Section {0} not found in the {1} file'.format(section, filename))
    
        return db
    

    Note: sql.Identifier will add quotes if needed so it will work if you use quoted identifiers in PostgreSQL also (they have to be used to allow e.g. case sensitive naming).

    Example and structure of database.ini:

    [postgresql]
    host=localhost
    port=5432
    database=postgres
    user=user
    password=mypass
    
    0 讨论(0)
  • 2020-12-24 11:25

    this question is old and maybe there is a newer one out there, but the answer my colleagues are going with right now is this:

    sql = "SELECT * FROM table WHERE column = ANY(%(parameter_array)s)"
    cur.execute(sql,{"parameter_array": [1, 2, 3]})
    
    0 讨论(0)
  • 2020-12-24 11:31

    Python tuples are converted to sql lists in psycopg2:

    cur.mogrify("SELECT * FROM table WHERE column IN %s;", ((1,2,3),))
    

    would output

    'SELECT * FROM table WHERE column IN (1,2,3);'
    

    For Python newcomers: It is unfortunately important to use a tuple, not a list here. Here's a second example:

    cur.mogrify("SELECT * FROM table WHERE column IN %s;", 
        tuple([row[0] for row in rows]))
    
    0 讨论(0)
提交回复
热议问题