1. 程式人生 > 實用技巧 >實時電商數倉(二十二)之實時計算(一)日活處理模組(一)

實時電商數倉(二十二)之實時計算(一)日活處理模組(一)

import pymysql
def connect_mysql():
conn = pymysql.connect("ip", "使用者名稱", "密碼", "需要連線的資料庫", charset='utf8')
cursor = conn.cursor()
# sql = """CREATE TABLE `check_kafka_status`(
# `id` INT primary key AUTO_INCREMENT COMMENT 'ID',
# `group_id` varchar(40) COMMENT 'kafka的group_id',
# `code` varchar(4) COMMENT '0不變,1增加,-1減少',
# `exited` varchar(200) COMMENT '已經啟動的docker,列表形式',
# `adding` varchar(40) COMMENT '需要增加的docker',
# `reduce` varchar(40) COMMENT '需要減少的docker',
# `update_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '更新時間'
# ) ENGINE=InnoDB CHARSET=utf8 COMMENT='檢測kafka資料積壓狀態';
# """
sql = "insert check_kafka_status (group_id, code) value ('coupon_update_v1', '0')"
# sql2 = "select exited from check_kafka_status"
try:
cursor.execute(sql)
conn.commit()
# cursor.execute(sql2)
# result = cursor.fetchone()
# print(result)
except Exception as e:
print(e)
conn.rollback()
cursor.close()
conn.close()
if __name__ == '__main__':
connect_mysql()