Remove the Description field from the script table as the file name and place the script in the same folder as the protocol and write the contents of the Testscript field to the file
ImportMySQLdbImportSYSImportOS fromlxmlImportEtreeip=sys.argv[1]#ip= "10.0.20.252"Dir="Scriptsdir"Try: Print "connnect to database ...."DB=mysqldb.connect (IP,"Root","free-wi11","acheron_db")exceptException as E:PrintE.messagePrint "Can ' t connect to"+IP exit (1)Print "Done ...."Print "Ready-to- read data from DataBase ..."Db_cursor=db.cursor () SQL="Select Description,testscript from Script where class= ' System '"db_cursor.execute (SQL) content=Db_cursor.fetchall ()ifos.path.exists (DIR): Os.system ("rd/s/q"+DIR) Os.system ("mkdir"+DIR) forIteminchContent:file_name=Item[0]ifLen (file_name) = =0:PrintItemContinueDirectory=item[0].split (".") [0]ifLen (directory) = =0:PrintItemContinue if notOs.path.exists (dir+"\\"+directory): Os.system ("mkdir"+ DIR +"\\"+directory)Try: F=open (dir+"\\"+directory+"\\"+file_name+". XML","W") Root=etree.fromstring (item[1]) F.write ("<?xml version=\ "1.0\" encoding=\ "utf-8\"? >\n") F.write (etree.tostring (Root)) F.close ()exceptException as E:PrintE.messagePrint "failed,please retry ...." Break;Print "See Scripts data file in '"+ DIR +"'"Print " Done"
Execution Result:
Remove the data from the user table from the MyBatis database on the 10.0.10.33 to the local User.txt folder
Look at the user table:
ImportMySQLdb#Ip=sys.argv[1]ip="10.0.10.33"FileName="User.txt"Try: Print "connnect to database ...."DB=mysqldb.connect (IP,"Root","123456","MyBatis")exceptException as E:PrintE.messagePrint "Can ' t connect to"+IP exit (1)Print "Done ...."Print "Ready-to- read data from DataBase ..."Db_cursor=db.cursor () SQL="SELECT * from Users"db_cursor.execute (SQL) content=Db_cursor.fetchall () F=open (FileName,"W") forIteminchcontent:Try: #root=etree.fromstring (item[1])F.write ("ID:"+ str (item[0]) +"Name:"+ item[1] +"Age :"+ STR (item[2])) F.write ("\ r") exceptException as E:PrintE.messagePrint "failed,please Retry ..." Breakf.close ()Print "See user list in '"+ FileName +"'"Print " Done"
Execution Result:
C:\Python27\python.exe g:/web2py/web2py_win/web2py/applications/pythonmysqldb/ from inch ' User.txt ' Doneprocess finished with exit code 0
Python script connection database read specific fields saved in file