如何插入'进入postgresql?

时间:2016-10-13 11:08:22

标签: python postgresql psycopg2

我使用pythonpsycopg2来更新数据库。

cur.execute("UPDATE scholars SET name='{}' WHERE id={} and name is null".format(author, scholar_id))

psycopg2.ProgrammingError: syntax error at or near "Neill" LINE 1: UPDATE scholars SET name='O'Neill, Kevin' WHERE id=12403 and...

数据应为:O'Neill, Kevin

1 个答案:

答案 0 :(得分:1)

使用Psycopg's parameter passing functionality

cur.execute ("""
    UPDATE scholars 
    SET name = %s 
    WHERE id = %s and name is null
    """,
    (author, scholar_id)
)

Triple quotes make it clearer

相关问题