动态更新PostgreSQL数据表中的Python烧瓶中的空格

时间:2020-07-05 14:35:48

标签: python flask postgresql-9.4

我的查询是

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

在烧瓶路线中,我正在使用此查询,

  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))

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

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

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"

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