【问题标题】:Dynamic update in postgresql table data with spaces in python flask在 python 烧瓶中使用空格动态更新 postgresql 表数据
【发布时间】:2020-07-05 14:35:48
【问题描述】:

我的查询是,

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

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

谁能帮帮我?

【问题讨论】:

    标签: python flask postgresql-9.4


    【解决方案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 发送到服务器的查询:它实际上并不执行查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 2020-04-16
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      相关资源
      最近更新 更多