在 postgresql table 数据中动态更新 python flask 中的空格

Dynamic update in postgresql table data with spaces in python flask

我的查询是,

  engine = create_engine("postgres://")
  conn = engine.connect()
  conn.autocommit = True

我在 Flask Route 中使用这个查询,

  result = conn.execute("""UPDATE business_portal SET business_name ="""+str(business_name)+""", name_tag ="""+str(business_tag)+""",name_atr = """+str(business_attr)+""", address =""" +str(address)+""",address_tag =""" +str(address_tag)+""", address_atr = """+str(address_attr)+""", city = """+str(city)+""", city_tag ="""+str(city_tag)+""", city_atr =""" +str(city_attr)+""", state = """+str(state)+""", state_tag = """+str(state_tag)+""",state_atr = """+str(state_attr)+""",zip_code = """+str(zipcode)+""",zip_tag ="""+str(zip_tag)+""",zip_atr ="""+str(zip_attr)+""",contact_number ="""+str(contact_number)+""",num_tag = """+str(contact_tag)+""", num_atr ="""+str(contact_attr)+""",domain ="""+str(domain)+""", search_url = """+str(search_url)+""",category =""" +str(category)+""", logo_path =""" +str(logo_path)+""" WHERE id=%s """,(id))

上面的查询接受不带 space 的数据(例如 abcd)....但是当数据带有 spaces(例如 abcd efgh ijkl)时,它会显示语法错误。

谁能帮帮我?

SET 子句中的值需要以与 WHERE 子句中的值相同的方式引用。

>>> cur = conn.cursor()
>>> stmt = "UPDATE tbl SET col = %s WHERE id = %s"
>>>
>>> # Observe that the SET value is three separate characters
>>> cur.mogrify(stmt % ('a b c', 37))
b'UPDATE tbl SET col = a b c WHERE id = 42'
>>>
>>> # Observe that the SET value is a single, quoted value
>>> cur.mogrify(stmt,  ('a b c', 37))
b"UPDATE tbl SET col = 'a b c' WHERE id = 42"

注意 cursor.mogrify 是一种 psycopg2 方法,它打印将由 cursor.execute 发送到服务器的查询:它实际上并不执行查询。