定时插入数据测试脚本

Posted wonder4

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了定时插入数据测试脚本相关的知识,希望对你有一定的参考价值。

insert.py
import cx_Oracle
class Oracle(object):
“”" oracle db operator “”"
def init(self,userName,password,host,instance):
self._conn = cx_Oracle.connect("%s/%s@%s/%s" % (userName,password,host,instance))
self.cursor = self._conn.cursor()
def queryTitle(self,sql,nameParams=):
if len(nameParams) > 0 :
self.cursor.execute(sql,nameParams)
else:
self.cursor.execute(sql)
colNames = []
for i in range(0,len(self.cursor.description)):
colNames.append(self.cursor.description[i][0])
return colNames
# query methods
def queryAll(self,sql):
self.cursor.execute(sql)
return self.cursor.fetchall()
def queryOne(self,sql):
self.cursor.execute(sql)
return self.cursor.fetchone()
def queryBy(self,sql,nameParams=):
if len(nameParams) > 0 :
self.cursor.execute(sql,nameParams)
else:
self.cursor.execute(sql)
return self.cursor.fetchall()
def insertBatch(self,sql,nameParams=[]):
“”“batch insert much rows one time,use location parameter”""
self.cursor.prepare(sql)
self.cursor.executemany(None, nameParams)
self.commit()
def commit(self):
self._conn.commit()
def del(self):
if hasattr(self,‘cursor’):
self.cursor.close()
if hasattr(self,’_conn’):
self.conn.close()
def test1():
# sql = “”“select user_name,user_real_name,to_char(create_date,‘yyyy-mm-dd’) create_date from sys_user where id = ‘10000’ “””
sql = “”“select user_name,user_real_name,to_char(create_date,‘yyyy-mm-dd’) create_date from sys_user where id =: id “””
oraDb = Oracle(‘test’,‘java’,‘192.168.0.192’,‘orcl’)
fields = oraDb.queryTitle(sql, ‘id’:‘10000’)
print(fields)
print(oraDb.queryBy(sql, ‘id’:‘10000’))
def test2():
oraDb = Oracle(‘test’,‘test’,‘129.154.102.100’,‘PDB1.612739963.oraclecloud.internal’)
cursor = oraDb.cursor
#drop_table=""“drop table python_modules”""
#cursor.execute(drop_table)
import random
lat = “”"python_modules
"""+str(random.randint(1,4000))
create_table = “”"
CREATE TABLE “”"+str(lat)+""" (
module_name VARCHAR2(50) NOT NULL,
file_path VARCHAR2(300) NOT NULL
)
“”"
from sys import modules
cursor.execute(create_table)
M = []
for m_name, m_info in modules.items():
try:
M.append((m_name, m_info.file))
except AttributeError:
pass
sql = “INSERT INTO “+lat+”(module_name, file_path) VALUES (:1, :2)”
oraDb.insertBatch(sql,M)
cursor.execute("SELECT COUNT(*) FROM "+lat)
print(cursor.fetchone())
print(‘insert batch ok.’)
#cursor.execute(“DROP TABLE python_modules PURGE”)

insert.sh
python insert.py

run.sh
#!/bin/bash
#需要定时执行的程序
program=./insert.sh
#获取当前时间,例如20171129 <—> perDate=KaTeX parse error: Expected 'EOF', got '#' at position 18: …ate "+%Y%m%d") #̲获取一天后的时间 afterd…(date -d +1day “+%Y%m%d”)
#afterdata=20170323
#每五分钟仅仅执行一次,设置标志位
onceflag=0
echo ‘Task schedule Time: (’KaTeX parse error: Expected 'EOF', got '#' at position 53: … per 5min)...' #̲死循环,当检测到当前时间段分钟…(date “+%M”);
if [ expr $presentminutes % 120 -eq 0 ]
then
if [ o n c e f l a g − e q 0 ] t h e n e c h o ′ N o w m i n u t e i s ′ onceflag -eq 0 ] then echo &#x27;Now minute is &#x27; onceflageq0]thenechoNowminuteispresentminutes’’
echo ‘The program (’$program’) running…’
echo $program $afterdata #打印测试可执行程序格式是否正确
$program a f t e r d a t a e c h o ′ T h e p r o g r a m ( ′ afterdata echo &#x27;The program (&#x27; afterdataechoTheprogram(program’) stopped…’
onceflag=1
else
continue
fi
else
onceflag=0
fi
sleep 1 #一秒查询一次当前时间点
done

以上是关于定时插入数据测试脚本的主要内容,如果未能解决你的问题,请参考以下文章

springboot注销30天后清除数据怎么实现

怎么创建linux账户90天后过期

Linux - crontab 定时任务

为啥我的长时间运行的 python 脚本在运行大约 3 天后会因“无效指针”而崩溃?

Linux下定时备份MySQL数据库的Shell脚本

shell写定时任务