MySQL Connector/Python 发行说明
语法
cursor.add_attribute(name, value)
将新的命名查询属性添加到列表中,作为 MySQL 服务器的 查询属性 功能的一部分。
name
: 名称必须是字符串,但不会进行其他验证检查;属性将按原样发送到服务器,任何错误都将由服务器检测并报告。
value
: 转换为 MySQL 二进制协议的值,类似于准备好的语句参数的转换方式。如果转换失败,则会报告错误。
查询属性必须在服务器上启用,默认情况下禁用。当为不支持查询属性的服务器连接设置查询属性时,会记录警告。有关启用 query_attributes MySQL 服务器组件的信息,请参阅 使用查询属性的先决条件。
查询属性使用示例
# Each invocation of `add_attribute` method will add a new query attribute:
cur.add_attribute("foo", 2)
cur.execute("SELECT first_name, last_name FROM clients")
# The query above sent attibute "foo" with value 2.
cur.add_attribute(*("bar", "3"))
cur.execute("SELECT * FROM products WHERE price < ?", 10)
# The query above sent attibutes ("foo", 2) and ("bar", "3").
my_attributes = [("page_name", "root"), ("previous_page", "login")]
for attribute_tuple in my_attributes:
cur.add_attribute(*attribute_tuple)
cur.execute("SELECT * FROM offers WHERE publish = ?", 0)
# The query above sent 4 attibutes.
# To check the current query attributes:
print(cur.get_attributes())
# prints:
[("foo", 2), ("bar", "3"), ("page_name", "root"), ("previous_page", "login")]
# Query attributes are not cleared until the cursor is closed or
# of the clear_attributes() method is invoked:
cur.clear_attributes()
print(cur.get_attributes())
# prints:
[]
cur.execute("SELECT first_name, last_name FROM clients")
# The query above did not send any attibute.
此方法在 Connector/Python 8.0.26 中添加。