psycopg2 equivalent of mysqldb.escape_string?

后端 未结 5 585
迷失自我
迷失自我 2020-12-10 23:53

I\'m passing some values into a postgres character field using psycopg2 in Python. Some of the string values contain periods, slashes, quotes etc.

With MySQL I\'d ju

5条回答
  •  感情败类
    2020-12-11 00:44

    psycopg2 added a method in version 2.7 it seems: http://initd.org/psycopg/docs/extensions.html#psycopg2.extensions.quote_ident

    from psycopg2.extensions import quote_ident
    
    with psycopg2.connect() as conn:
        with conn.cursor() as curs:
            ident = quote_ident('foo', curs)
    

    If you get an error like: TypeError: argument 2 must be a connection or a cursor, try either:

    ident = quote_ident('foo', curs.cursor)
    
    # or
    
    ident = quote_ident('food', curs.__wrapper__)
    
    

提交回复
热议问题