dataCreating/bin/SQLDataCreating.py

81 lines
1.9 KiB
Python
Raw Normal View History

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-25 13:47:36 +00:00
2023-10-26 04:02:41 +00:00
#获取连接信息
def get_db_info(DBType = "mysql"):
db_config = {}
if DBType == "mysql":
db_config = DBConfig.mysql_config()
elif DBType == "mysql":
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-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()
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条数据
@repeat(every(10).seconds)
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小时修改一下数据
@repeat(every(20).seconds)
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()