Use python to generate a sample code for exporting the database's bat script file

Source: Internet
Author: User
Example: # Environment: python3.xdefgetExportDbSql (db, index ): # obtain the SQL statement sqlmysqldump-u % s-p % s-h % s-P % d -- default-character-setutf8 -- databasesmu_ins_s % s & gt; % s. s % d. mu_ins _ % d. SQL % (db [user], db [pwd], db [host], db [port], ind This article shares the sample code for generating a bat script file for exporting the database using python.

Example:

# Environment: python3.xdef getExportDbSql (db, index ): # obtain the SQL statement SQL = 'mysqldump-u % s-p % s-h % s-P % d -- default-character-set = utf8 -- databases mu_ins_s for exporting a database instance % s> % s. s % d. mu_ins _ % d. SQL '% (db ['user'], db ['pwd'], db ['host'], db ['port'], index, db ['server'], index, index) return sqldef createDbBackupFile (fname, dbList): # Generate the database export statement and save it to the file if not fname or not dbList: return Falsef = open (fname, 'w') if f: f. write ('echo @ off \ n \ n') for db in dbList: for index in db ['indexlist']: f. write ('REM % s. s % d \ n' % (db ['server'], index) f. write ('% s \ n \ n' % getExportDbSql (db, index) f. write ('\ n') f. close () return Truereturn Falsedef initDb (user, pwd, host, port, server_name, indexList ): # generate a db dictionary object and return db ={} db ['user'] = userdb ['pwd'] = pwddb ['host'] = hostdb ['port'] = portdb ['server'] = server_namedb ['indexlist'] = indexListreturn dbdef displayDb (db): print ('User = ', db ['user']) print ('pwd =', db ['pwd']) print ('host = ', db ['host']) print ('Port = ', db ['port']) print ('server =', db ['server']) print ('indexlist = ', db ['indexlist']) print (' \ n') def displayList (list): for item in list: displayDb (item) if _ name _ = '_ main _': db1 = initDb ('root', '123', '123. 0.0.1 ', 3306, 'th1', [10000, 1, 3]) db2 = initDb ('root', '123', '123. 0.0.1 ', 3306, '2s', [10000, 1]) dbList = [] dbList. append (db1) dbList. append (db2) # displayList (dbList) createDbBackupFile ('export00. bat ', dbList)

The above is the details of the sample code for generating an exported database bat script file using python. For more information, see other related articles in the first PHP community!

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.