本文介紹如何使用 Python SDK 刪除Table Store資料表的單行資料。
前提條件
方法說明
def delete_row(self, table_name, row=None, condition=None, return_type=None, transaction_id=None, **kwargs)範例程式碼
以下範例程式碼用於刪除 test_table 表中主索引值為 row1 的行資料。
try:
# 構造主鍵
primary_key = [('id', 'row1')]
# 構造刪除的行資料
row = Row(primary_key)
# 調用 delete_row 方法刪除行資料
consumed, return_row = client.delete_row('test_table', row)
print('Read CU Cost: %s' % consumed.read)
print('Write CU Cost: %s' % consumed.write)
except Exception as e:
print("Delete row failed with error: %s" % e)常見問題
Table StorePython SDK 6.0.0刪除單行資料報錯
推薦以下兩種方式解決此問題。
升級Table StorePython SDK的版本。
修改刪除資料方法的參數。
# 設定資料表名稱。 table_name = '<TABLE_NAME>' # 構造行主鍵。 primary_key = [('gid', 1), ('uid', '101')] condition = Condition('IGNORE') try: consumed, return_row = client.delete_row(table_name, primary_key, condition) print('Delete succeed, consume %s write cu.' % consumed.write) # 用戶端異常,一般為參數錯誤或者網路異常。 except OTSClientError as e: print("Delete row failed, http_status:%d, error_message:%s" % (e.get_http_status(), e.get_error_message())) # 服務端異常,一般為參數錯誤或者流控錯誤。 except OTSServiceError as e: print("Delete row failed, http_status:%d, error_code:%s, error_message:%s, request_id:%s" % ( e.get_http_status(), e.get_error_code(), e.get_error_message(), e.get_request_id()))