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