发布网友 发布时间:2022-04-08 01:28
共4个回答
懂视网 时间:2022-04-08 05:49
Python学习第二弹
#coding = UTF-8 import os, sys, time, shutil class NdbFlush: def __init__(self): self._ROOT_PATH_ = None self._TNS_LIST_ = {} self._FILE_LIST_ = {} # {SCHEMA:{‘TAB‘:[], ‘SEQ‘:[], ‘PKGH‘:[]}, SCHEMA:{...}, ...} self._ORA_CFG_FILE_ = None def _UnInit_(self): self._ROOT_PATH_ = None self._TNS_LIST_ = None self._FILE_LIST_ = None self._ORA_CFG_FILE_ = None ####################################### # 设置路径 ####################################### def GetFilePath(self): expath = os.getcwd() if os.path.isdir(expath): return expath elif os.path.isfile(expath): return os.path.dirname(expath) ####################################### # 获取连接串列表 ####################################### def GetOraInfo(self): ora_file = self._ROOT_PATH_ + ‘/‘ + self._ORA_CFG_FILE_ tns_list = {} fh = open(ora_file, ‘r‘) for ora in fh.readlines(): ora = ora.replace(‘ ‘, ‘‘) if ora and len(ora) > 5: ora_list = [] schema = ora.split(‘/‘)[0].upper() ora_list = tns_list.get(schema) if (ora_list and len(ora_list) > 0): ora_list.append(ora) else: ora_list = [ora] tns_list[schema] = ora_list return tns_list ####################################### # 将SQL文件名称加载到列表中 ####################################### def LoadNdbList(self, _list_): #print(‘[LoadNdbList] _list_ =‘, _list_, ‘ len(_list_)‘, len(_list_)) lst = {} try: for itm in _list_: #print(‘ ITM =‘, itm) file_split = itm.split(‘_‘) #print(‘ file_split[0] =‘, file_split[0], ‘ file_split[1] =‘, file_split[1]) #schema = {} files = [] schema = lst.get(file_split[1].upper()) #print(‘ schema =‘, schema) if schema and len(schema) > 0: files = schema.get(file_split[0]) #print(‘ files =‘, files) if files and len(files) > 0: files.append(itm) schema[file_split[0]] = files #print(‘ files 1 =‘, files) #print(‘ schema =‘, schema) else: files = [itm] schema[file_split[0]] = files #print(‘ files 2 =‘, files) else: #print(‘ ‘, schema, file_split[0], itm) schema = {} files =[itm] schema[file_split[0]] = files #print(‘ schema =‘, schema) lst[file_split[1].upper()] = schema #print(‘ LST =‘, lst) #print(‘-‘ * 80) except Exception as e: #print(‘请传入数组类参数(如:元组[]) ‘) print(e) lst = {} #print(lst) return lst ####################################### # 获取SQL文件列表 ####################################### def GetSqlFileList(self): filelist = [] sqlPath = self._ROOT_PATH_ + ‘/files/‘ for file in os.listdir(sqlPath): if file[-4:].upper() == ‘.SQL‘: if filelist: filelist.append(file) else: filelist = [file] #print(‘ ‘, filelist, ‘ ‘) print(‘在目录[‘ + sqlPath + ‘]下找到[%d]个SQL文件‘ % len(filelist)) return filelist ####################################### # 将SQL刷入DB ####################################### def SqlFlushDB(self, _runMode_ = ‘M‘): # schema loop file_list = self._FILE_LIST_ ora_list = self._TNS_LIST_ filePath = self._ROOT_PATH_ + ‘/files/‘ for im in file_list: #print(‘schema =‘, im)#, lst.get(im)) # file type loop begin # SEQ for xm in file_list.get(im): #print(‘ type =‘, xm, ‘ list =‘, file_list.get(im).get(xm)) if ‘SEQ‘ == xm.upper(): self.InitBat(im, file_list, ora_list, filePath, xm, _runMode_) # TAB for xm in file_list.get(im): #print(‘ type =‘, xm, ‘ list =‘, file_list.get(im).get(xm)) if ‘TAB‘ == xm.upper(): self.InitBat(im, file_list, ora_list, filePath, xm, _runMode_) # TAB for xm in file_list.get(im): #print(‘ type =‘, xm, ‘ list =‘, file_list.get(im).get(xm)) if ‘PKGH‘ == xm.upper(): self.InitBat(im, file_list, ora_list, filePath, xm, _runMode_) # not in (TAB, SEQ) for xm in file_list.get(im): #print(‘ type =‘, xm, ‘ list =‘, file_list.get(im).get(xm)) if ‘TAB‘ != xm.upper() and ‘SEQ‘ != xm.upper() and ‘PKGH‘ != xm.upper(): self.InitBat(im, file_list, ora_list, filePath, xm, _runMode_) # file type loop end def InitBat(self, _schema_, _fileList_, _oraList_, _filePath_, _fileType_, _runMode_ = ‘M‘): # file name loop for file in _fileList_.get(_schema_).get(_fileType_): #print(‘ ‘, file) filePath = _filePath_ sqlpath = filePath + file fh = open(sqlpath, ‘a+‘) fh.write(‘ exit‘) fh.close() tnslst = ‘‘ # ora conf loop fht = open(sqlpath + ‘.bat‘, ‘a+‘) fht.write(‘title [‘ + file + ‘] echo off ‘) fht.write(‘cd ‘ + filePath + ‘ ‘) fht.write(‘cls ‘) # tns loop for tns in _oraList_.get(_schema_): #print(‘ ‘, tns) tnslst += tns + ‘, ‘ fht.write((‘@echo "[ %s ]‘ %file) + (‘ -> [ %s]"‘ %tns) + ‘ ‘) fht.write(‘@echo 刷库中... ‘) fht.write(‘sqlplus ‘ + tns + ‘ @‘ + file + ‘ >> ‘ + file + ‘.log ‘) fht.flush() #fht.write(‘@pause ‘) fht.write(‘@echo FINISH>‘ + file + ‘.ok‘) fht.write(‘ exit‘) fht.close() print((‘[ %s ]‘ %file) + (‘ -> [ %s]‘ %tnslst)) if _runMode_ == ‘M‘: self.RunBat(sqlpath, _runMode_) else: os.system(r‘‘ + sqlpath + ‘.bat‘) #time.sleep(1) try: fhl = open(r‘‘ + sqlpath + ‘.log‘, ‘r‘) lines = fhl.readlines() lineidx = 0 errFlag = False fhl.close() for line in lines: lineU = line.upper() if lineU.find(‘ERROR‘) >= 0: errFlag = True break lineidx += 1 if errFlag: print(‘ >>[Status] Failed..‘) print(‘ [ ‘ + lines[lineidx].replace(‘ ‘, ‘‘) + ‘ ]‘) print(‘ [ ‘ + lines[lineidx + 1].replace(‘ ‘, ‘‘) + ‘ ]‘) else: print(‘ >>[Status] Success..‘) if os.path.isfile(r‘‘ + sqlpath + ‘.log‘): os.remove(r‘‘ + sqlpath + ‘.log‘) shutil.move(sqlpath, sqlpath.replace(‘/files/‘, ‘/finish/‘)) except Exception as e: print(‘ 程序异常:‘, e) print(‘-‘ * 70) def RunBat(self, _fileName_, _runMode_): state = ‘START‘ while True: #print(runnext) if state == ‘START‘: os.system(‘start ‘ + r‘‘ + _fileName_ + ‘.bat‘) state = ‘RUNNING‘ #print(1) elif state == ‘FINISH‘: #print(9) break elif state == ‘RUNNING‘: time.sleep(1) #print(2) try: fh = open(r‘‘ + _fileName_ + ‘.ok‘, ‘r‘) state = fh.read().replace(‘ ‘, ‘‘) except: state = ‘RUNNING‘ else: break def CleanFile(self, _mode_ = ‘Finish‘): tmpPath = self._ROOT_PATH_ + ‘/files/‘ for file in os.listdir(tmpPath): ffff = file.upper() delFlag = False if _mode_ == ‘Finish‘: if ffff[-4:] == ‘.BAT‘ or ffff[-7:] == ‘.SQL.OK‘: delFlag = True else: if ffff[-4:] == ‘.LOG‘ or ffff[-4:] == ‘.BAT‘ or ffff[-7:] == ‘.SQL.OK‘: delFlag = True if delFlag: tmpFile = os.path.join(tmpPath, file) if os.path.isfile(tmpFile): os.remove(tmpFile) def Launcher(self): #l = [‘tab_lpms_xxx.sql‘, ‘tab_lpms_xx1x.sql‘, ‘tab_lpms_xxxxx.sql‘, ‘tab_wlt_xxx.sql‘, ‘seq_lpms_xxx.sql‘, ‘pkgh_jone_xxx.sql‘, ‘pkgb_jone_xxx.sql‘, ‘pubk_jone_xxx.sql‘] #self._FILE_LIST_ = self.LoadNdbList(l) # 清理历史bat文件 self._ROOT_PATH_ = self.GetFilePath().replace(‘\‘, ‘/‘) print(‘工作目录:‘, self._ROOT_PATH_) _clean_ = True _show_list_ = False _dosMode_ = ‘M‘ #workp = ‘D:/NdbFlush‘ ipt = input(‘>>‘) ipt = ipt.upper() if ipt == ‘V‘: _show_list_ = True elif ipt == ‘D1‘: _dosMode_ = ‘S‘ elif ipt == ‘D2‘: _dosMode_ = ‘M‘ elif ipt == ‘Q‘: exit() elif ipt == ‘C‘: _clean_ = True else: print(‘将以默认方式执行...‘) print(‘=‘ * 70) print(‘ ‘) self.CleanFile(‘Begin‘) self._FILE_LIST_ = self.LoadNdbList(self.GetSqlFileList()) # 显示文件清单 BEGIN if _show_list_: lst = self._FILE_LIST_ for im in lst: # schema print(‘schema =‘, im)#, lst.get(im)) for xm in lst.get(im): # file type print(‘ type =‘, xm, ‘ list =‘, lst.get(im).get(xm)) # 显示文件清单 END self._ORA_CFG_FILE_ = ‘ora_tns_info.conf‘ self._TNS_LIST_ = self.GetOraInfo() self.SqlFlushDB(_dosMode_) if _clean_: self.CleanFile() self._UnInit_() return self._FILE_LIST_, self._TNS_LIST_ def usage(): print(‘=‘ * 70) print(‘= [ NDB 刷库工具 ] v1.0‘) print(‘= 2015-07-05 by L‘) print(‘-‘ * 60) print(‘= [ Q: 退出; ]‘) print(‘= [ V: 显示录库的SQL列表; ]‘) print(‘= [ D1: 单窗口执行; D2:多窗口执行[默认]; ]‘) print(‘= [ 执行前请确保: ‘) print(‘= 1)SQL:[$WorkPath$/files/]‘) print(‘= 2)TNS:[$WorkPath$/ora_tns_info.conf]‘) print(‘= [ 回车继续; ]‘) print(‘=‘ * 70) if __name__ == ‘__main__‘: usage() ndb = NdbFlush() lst, ora = ndb.Launcher() print(‘ ‘) print(‘=‘ * 70) print(‘ ‘) print(‘刷库动作完毕,执行结果详见上面日志,失败信息已写入对应log文件$WorkPath$/file/*.log... [Enter]‘) input(‘‘) ‘‘‘ print(‘[MAIN] lst =‘, lst) print(‘[MAIN] ora =‘, ora) print(‘ ‘) print(‘ ‘) print(‘-‘ * 100) print(‘ ‘) for im in lst: # schema print(‘schema =‘, im)#, lst.get(im)) for xm in lst.get(im): # file type print(‘ type =‘, xm, ‘ list =‘, lst.get(im).get(xm)) ‘‘‘
使用Python将sql文件刷入DB
标签:python sql刷库
热心网友 时间:2022-04-08 02:57
python把数据库查询结果写入文件的例子如下:热心网友 时间:2022-04-08 04:15
应该没有,但你可以:
fp = file.open('F:/xxx.txt','wb')这样也可以达到你的目的
如果你想每次写入一个新的文件,就这样
filename = str(datetime.now()).replace(" ",'_').replace(":",'_') +每次写入一个新建的txt,名字为当前时间加随机数命名
热心网友 时间:2022-04-08 05:50
如果用的是mysql的话,OUTFILE就是把结果输出到文件的追问sql = 'SELECT * INTO OUTFILE "xxx.txt" FIELDS TERMINATED BY "\t" FROM table LIMIT 3;'