2023-10-25 10:38:23 +00:00
|
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
|
import _load
|
|
|
|
|
|
2023-10-26 04:02:41 +00:00
|
|
|
|
import DBConfig
|
2023-10-25 10:38:23 +00:00
|
|
|
|
|
|
|
|
|
import jaydebeapi
|
2023-10-26 04:02:41 +00:00
|
|
|
|
|
2023-10-25 10:38:23 +00:00
|
|
|
|
from schedule import every, repeat, run_pending
|
|
|
|
|
|
|
|
|
|
|
2023-10-26 05:14:46 +00:00
|
|
|
|
"""
|
|
|
|
|
说明:
|
|
|
|
|
1. 用于数据库的持续增、删、改,目前支持MySQL和Oracle
|
|
|
|
|
2. DBConfig.py 为数据库连接配置,使用时需要修改连接信息
|
|
|
|
|
3. 脚本运行会创建一个测试表
|
|
|
|
|
4. 每秒往测试表中插入1条数据
|
|
|
|
|
5. 每5分钟删除100条数据
|
|
|
|
|
6. 每1小时,修改一下数据
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# 目前支持 MySQL和Oracle两种数据库,其他的数据库需要做适配
|
2023-11-07 10:01:16 +00:00
|
|
|
|
DBType = "mysql"
|
2023-10-26 05:14:46 +00:00
|
|
|
|
|
2023-10-25 13:47:36 +00:00
|
|
|
|
|
2023-10-26 04:02:41 +00:00
|
|
|
|
#获取连接信息
|
2023-10-26 05:14:46 +00:00
|
|
|
|
def get_db_info(dbtype=DBType):
|
2023-10-26 04:02:41 +00:00
|
|
|
|
db_config = {}
|
2023-10-26 05:14:46 +00:00
|
|
|
|
if dbtype == "mysql":
|
2023-10-26 04:02:41 +00:00
|
|
|
|
db_config = DBConfig.mysql_config()
|
2023-10-26 05:14:46 +00:00
|
|
|
|
elif dbtype == "oracle":
|
2023-10-26 04:02:41 +00:00
|
|
|
|
db_config = DBConfig.oracle_config()
|
|
|
|
|
else:
|
|
|
|
|
assert False ,"DBType is not support."
|
|
|
|
|
return(db_config)
|
|
|
|
|
|
|
|
|
|
#处理连接信息
|
|
|
|
|
db_config = get_db_info()
|
2023-10-25 13:47:36 +00:00
|
|
|
|
OAuth = [str(db_config["db_user"]), str(db_config["db_password"])]
|
|
|
|
|
if not db_config["db_user"] and not db_config["db_password"]:
|
2023-10-25 10:38:23 +00:00
|
|
|
|
OAuth = None
|
2023-10-25 13:47:36 +00:00
|
|
|
|
# conn to db by jaydebeapi
|
|
|
|
|
conn = jaydebeapi.connect(db_config["driver_name"],
|
|
|
|
|
db_config["jdbc_url"],
|
|
|
|
|
OAuth,
|
2023-10-26 04:02:41 +00:00
|
|
|
|
db_config["driver_jar_path"])
|
2023-10-25 13:47:36 +00:00
|
|
|
|
# conn.close()
|
2023-10-25 10:38:23 +00:00
|
|
|
|
|
2023-10-26 04:02:41 +00:00
|
|
|
|
#执行sql
|
2023-10-25 13:47:36 +00:00
|
|
|
|
def execute_sql(sql):
|
|
|
|
|
with conn.cursor() as curs:
|
|
|
|
|
curs.execute(sql)
|
2023-10-25 10:38:23 +00:00
|
|
|
|
|
2023-10-25 13:47:36 +00:00
|
|
|
|
#@repeat(every(1).hours)
|
2023-10-26 04:02:41 +00:00
|
|
|
|
#TODO(MH):按小时创建表,不太好控制,后面再去搞
|
2023-10-25 10:38:23 +00:00
|
|
|
|
def create_table():
|
2023-10-26 04:02:41 +00:00
|
|
|
|
print("start create table...")
|
|
|
|
|
sql_string = db_config["create_sql_string"]
|
2023-10-26 05:14:46 +00:00
|
|
|
|
|
2023-10-25 13:47:36 +00:00
|
|
|
|
execute_sql(sql_string)
|
2023-10-25 10:38:23 +00:00
|
|
|
|
|
|
|
|
|
|
2023-10-26 04:02:41 +00:00
|
|
|
|
#每秒插入1条数据
|
|
|
|
|
@repeat(every(2).seconds)
|
2023-10-25 10:38:23 +00:00
|
|
|
|
def insert_data():
|
2023-10-26 04:02:41 +00:00
|
|
|
|
print("insert data ....")
|
|
|
|
|
#重新获取一下,确保每次的插入的数据都不一样
|
|
|
|
|
db_config2 = get_db_info()
|
2023-11-14 05:43:44 +00:00
|
|
|
|
print("insert data ...."+db_config2["jdbc_url"])
|
2023-10-26 04:02:41 +00:00
|
|
|
|
insert_list = db_config2["insert_list"]
|
|
|
|
|
#执行插入
|
|
|
|
|
for sql_string in insert_list:
|
|
|
|
|
execute_sql(sql_string)
|
2023-10-25 10:38:23 +00:00
|
|
|
|
|
|
|
|
|
|
2023-10-26 04:02:41 +00:00
|
|
|
|
#每5分钟删除100条数据
|
2023-10-26 05:14:46 +00:00
|
|
|
|
@repeat(every(5).minutes)
|
2023-10-25 10:38:23 +00:00
|
|
|
|
def dalete_data():
|
2023-10-26 04:02:41 +00:00
|
|
|
|
print("dalete data....")
|
|
|
|
|
sql_string = db_config["del_sql"]
|
|
|
|
|
execute_sql(sql_string)
|
2023-10-25 10:38:23 +00:00
|
|
|
|
|
|
|
|
|
|
2023-10-26 04:02:41 +00:00
|
|
|
|
#每1小时,修改一下数据
|
2023-10-26 05:14:46 +00:00
|
|
|
|
@repeat(every(1).hours)
|
2023-10-25 10:38:23 +00:00
|
|
|
|
def updata_data():
|
2023-10-26 04:02:41 +00:00
|
|
|
|
print("updata data ....")
|
|
|
|
|
sql_string = db_config["updata_sql"]
|
|
|
|
|
execute_sql(sql_string)
|
|
|
|
|
|
2023-10-25 10:38:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-10-26 04:02:41 +00:00
|
|
|
|
create_table()
|
2023-10-25 10:38:23 +00:00
|
|
|
|
while True:
|
|
|
|
|
run_pending()
|