全部產品
Search
文件中心

Tablestore:刪除單行資料

更新時間:Mar 31, 2026

本文介紹如何使用 Python SDK 刪除Table Store資料表的單行資料。

前提條件

初始化Tablestore Client

方法說明

def delete_row(self, table_name, row=None, condition=None, return_type=None, transaction_id=None, **kwargs)

參數說明

名稱

類型

說明

table_name(必選)

str

資料表名稱。

row(必選)

Row

刪除的行資料資訊。

  • primary_key(必選)List[Tuple]:主鍵資訊,包括主鍵列名稱和主索引值。

    • 主鍵列資料類型包括 STRING、INTEGER 和 BINARY。

    • 主鍵個數和類型必須與資料表的主鍵保持一致。

condition(可選)

Condition

刪除條件,詳情請參見條件更新

return_type(可選)

ReturnType

傳回型別。

  • RT_NONE:預設值,不返回資料。

  • RT_PK:返回主鍵列。

transaction_id(可選)

str

局部事務ID,用於唯一標識局部事務,詳情請參見局部事務

範例程式碼

以下範例程式碼用於刪除 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()))

相關文檔

批次更新資料