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 中新增的。