Python script connection database read specific fields saved in file

Source: Internet
Author: User
Tags python script web2py

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.