python 運算元據庫

2021-08-21 17:39:14 字數 3970 閱讀 9179

目的:通過excel定義檢查指標項,然後通過python讀取指標,通過oracle sqlplus工具去執行獲取具體巡檢結果。

#unicode=utf-8

#-*- coding: utf-8 -*-

import os

import sys

import xlrd

import paramiko

reload(sys)

sys.setdefaultencoding('utf-8')

from subprocess import popen, pipe

os.environ['nls_lang'] = 'american_america.al32utf8'

sqlcommand = ''

tns=''

strcommand = ["sqlplus", "dbmgr/[email protected]:1521/dbmgr"]

formatstr = 'set linesize 120\n'

formatstr = formatstr + 'set pagesize 100\n'

formatstr = formatstr + 'set term off verify off feedback off tab off \n'

formatstr = formatstr + 'set numwidth 40\n'

formatstr = formatstr + sqlcommand+'\n'

defgettns

():print readexcels('e:\\db_checklist.xlsx','sheet2')

#開啟乙個workbook

workbook = xlrd.open_workbook('e:\\db_checklist.xlsx')

#抓取所有sheet頁的名稱

worksheets = workbook.sheet_names()

#print('worksheets is %s' %worksheets)

#定位到sheet1

worksheet1 = workbook.sheet_by_name('sheet1')

#遍歷sheet1中所有行row

defexcelrows

(): num_rows = worksheet1.nrows

for curr_row in range(num_rows):

row = worksheet1.row_values(curr_row)

#print('row%s is %s' %(curr_row,row))

#遍歷sheet1中所有列col

defexcelcols

(): num_cols = worksheet1.ncols

for curr_col in range(num_cols):

col = worksheet1.col_values(curr_col)

#print('col%s is %s' %(curr_col,col))

#遍歷sheet1中所有單元格cell

defreader

(worksheet):

sqls=

num_rows = worksheet.nrows

for rown in range(num_rows):

#for coln in range(num_cols):

cell = worksheet.cell_value(rown,0)

if str(cell).startswith('*.'):

cell='show parameter '+cell[2:]

continue

elif(str(cell).startswith('select')):

continue

else:

continue

#print type(sqls)

return sqls

defreadexcels

(path,ename):

workbook = xlrd.open_workbook(path)

worksheet = workbook.sheet_by_name(ename)

#print reader(worksheet1)

return reader(worksheet)

defred

(file):

files=open(file,'r')

lines=files.readlines()

for line in lines:

print line+'\n'

files.close()

return lines

defwrite

(content):

f=open(os.getcwd()+'\check.txt','a')

f.write(content)

f.close()

defgetsqls

():for i,sql in enumerate (readexcels('e:\\db_checklist.xlsx','sheet1')):

print

'\n'+'the index is %s, sql is %s ' %(i,sql)

return sql

defssh2

(ip,username,passwd,cmd):

try:

paramiko.util.log_to_file(os.getcwd()+'\paramiko.log')

ssh = paramiko.sshclient()

ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())

ssh.connect(ip,22,username,passwd,timeout=5)

for m in cmd:

stdin, stdout, stderr = ssh.exec_command(m)

# stdin.write("y") #

out = stdout.readlines()

print out

#stdin, stdout, stderr = ssh.exec_command('su - oracle')

#getsqlplus()

ssh.close()

except :

print

'%s\terror\n'%(ip)

"""#批量提取伺服器資訊

"""def

getsqlplus

():if (os.path.exists(os.getcwd()+'\check.txt')):

os.remove(os.getcwd()+'\check.txt')

for i,sql in enumerate (readexcels('e:\\db_checklist.xlsx','sheet1')):

print

'\n'+'the index is %s, sql is %s ' %(i,sql)

proc = popen(strcommand, stdout=pipe, stdin=pipe, stderr=pipe)

proc.stdin.write(sql)

(out, err) = proc.communicate()

if proc.returncode != 0:

print err

sys.exit(proc.returncode)

else:

#print 'end'

print out.split('sql>')[1]

write(out.split('sql>')[1])

print os.getcwd()+'\check.txt is created!'

getsqlplus()

python運算元據庫

資料庫的操作在現在的python裡面已經變得十分的好用,有了一套api標準.下面的就是講講如何的去使用這套框架定義.此框架包含以下部分 connect parameters.其中的引數格式如下 dsn 資料來源名稱 user 使用者名稱 可選 password 密碼 可選 host 主機名 可選 d...

python運算元據庫

python運算元據庫都是通過資料庫驅動取操作的。現在主要有兩張,一種是通過pymysql,還有一種是通過sqlalchemy。在這裡可能還會有人說還有mysqldb模組也可以操作。確實是的,但是mysqldb對python3已經不支援了,所以這裡我就不討論了。第一種pymysql pymysql幫...

python 運算元據庫

安裝第三方模組 pip install pymysql 資料處理流程 程式結構 python程式 mysql資料庫 引入依賴的模組 建立乙個連線mysql資料庫的連線物件通過連線物件獲得乙個操作sql語句的游標物件定義sql語句 通過游標物件執行sql語句 處理執行結果 關閉和資料庫的連線 實踐操作...