定时插入数据测试脚本

  • Post author:
  • Post category:其他



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;






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

























presentminutes’’

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;






a


f


t


e


r


d


a


t


a


e


c


h



o






















T


h


e


p


r


o


g


r


a


m



(

























program’) stopped…’

onceflag=1

else

continue

fi

else

onceflag=0

fi

sleep 1 #一秒查询一次当前时间点

done



版权声明:本文为wonder4原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。