import pandas as pd
import mysql.connector
from mysql.connector import Error

# 数据库连接信息
# host = "rm-uf6r230vbtxf5gdz63o.mysql.rds.aliyuncs.com"
# user = "rw_operation_data_relay"
# password = "m+q5Z4%IVuF9bf"
# database = "f6operation_data_relay"
# BI数据库链接配置-mysql
host = "f6-public.rwlb.rds.aliyuncs.com"
database = "f6operation_data_relay"
user = "rw_operation_data_relay"
password = "m+q5Z4%IVuF9bf"
table_name = "thailand_store_data_email"  # 要操作的表名
# table_name = "thailand_store_data_email" # 要操作的表名
start_id = 104864  # 要删除的区间起始ID
end_id = 106995    # 要删除的区间结束ID

# 连接数据库
try:
    connection = mysql.connector.connect(
        host=host,
        user=user,
        password=password,
        database=database
    )

    if connection.is_connected():
        cursor = connection.cursor()

        # 使用DELETE删除ID在指定区间内的数据
        delete_query = f"DELETE FROM {table_name} WHERE id BETWEEN {start_id} AND {end_id}"
        cursor.execute(delete_query)

        connection.commit()
        print(f"成功删除表 {table_name} 中ID在{start_id}到{end_id}之间的所有数据")

except Error as e:
    print(f"删除数据时发生错误: {e}")
    if connection.is_connected():
        connection.rollback()
finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("数据库连接已关闭")