[英]How do I insert this data into postgresql with python?
在以下代碼中,我想通過定位link
插入authors
。
self.cur.execute("""
UPDATE articles
SET authors = %s
WHERE link = %s returning id;
""" % (authors, link))
ret_id = self.cur.fetchone()
並且遇到兩個問題:
有些名稱不是這樣的常規名稱:
LINE 1: UPDATE articles SET authors = Francesco D'Angelo, Roberto T...
如果作者姓名沒有問題:
"""UPDATE articles SET authors = %s WHERE link = %s returning id;""" % (authors, link)) psycopg2.ProgrammingError: syntax error at or near ":" LINE 1: ...DATE articles SET authors = test WHERE link = http://www.wor...
使用SQL時不要使用%
格式化字符串,讓驅動程序進行適當的轉義:
self.cur.execute(
"""UPDATE articles SET authors = %s WHERE link = %s returning id;""", (authors, link))
注意,我將數據作為第二個參數傳遞, execute
將負責正確地轉義數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.