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!