如何将引号 marks/Apostrophe 添加到 psycopg2 中的列值?
How do I add quotation marks/Apostrophe's to column value in psycopg2?
information = "Hope they're well"
cursor.execute("UPDATE table_name SET information='%s';" % information)
添加它时显然会产生错误,因为执行只会尝试添加 'Hope they',然后字符串的其余部分会搞砸。
显然在 php 中有制作准备好的语句的选项,那么你如何在 psycopg2 中做到这一点?
我看了this,但不是很明白,也不知道这是不是我想做的。
不要格式化字符串,只需将您的值作为第二个参数传递即可:
cursor.execute("UPDATE mytable SET col1 = %s, col2 = %s;", [arg1, arg2])
可选地,使用命名参数并传递一个字典:
cursor.execute("""
UPDATE mytable
SET col1 = %(arg1)s, col2 = %(arg2)s
""",{'arg1':arg1, 'arg2':arg2})
psycopg2
会处理其他一切。
有关详细信息,请参阅 here。
information = "Hope they're well"
cursor.execute("UPDATE table_name SET information='%s';" % information)
添加它时显然会产生错误,因为执行只会尝试添加 'Hope they',然后字符串的其余部分会搞砸。
显然在 php 中有制作准备好的语句的选项,那么你如何在 psycopg2 中做到这一点?
我看了this,但不是很明白,也不知道这是不是我想做的。
不要格式化字符串,只需将您的值作为第二个参数传递即可:
cursor.execute("UPDATE mytable SET col1 = %s, col2 = %s;", [arg1, arg2])
可选地,使用命名参数并传递一个字典:
cursor.execute("""
UPDATE mytable
SET col1 = %(arg1)s, col2 = %(arg2)s
""",{'arg1':arg1, 'arg2':arg2})
psycopg2
会处理其他一切。
有关详细信息,请参阅 here。