import mysql.connector
import os
import pymysql
import shutil
print("输入数据库连接地址:")
host=input()
print("输入用户名:")
user=input()
print("输入密码:")
passwd=input()
#创建连接对象
mydb=mysql.connector.connect(
host=host,
user=user,
passwd=passwd
)
#创建指针对象
mycur=mydb.cursor()
print("展示此账号下所有数据库名:")
mycur.execute("show databases")
for x in mycur:
print(x)
print("选择要使用的数据库:")
database=input()
mycur.execute("use {}".format(database))
mycur.execute("show tables")
myresult1=mycur.fetchall()
for x in myresult1:
print(x)
print("选择要保存文件的地址:")
path = input()
print("选择要保存的文件名:")
filename = input()
if os.path.exists(path + './' + filename):
shutil.rmtree(path +'./' +filename,ignore_errors=True)
else:
pass
os.mkdir(path + './' + filename)
print("选择要到导出的结构类型:")
str=input()
if str=="表":
mycur.execute("select TABLE_NAME from information_schema.TABLES where TABLE_TYPE ='BASE TABLE' and TABLE_SCHEMA='{}'".format(database))
table=mycur.fetchall()
tablelens=len(table)
j=0
while j<tablelens:
a=(table[j])[0]
mycur.execute("show create table {}".format(a))
(myresult2, myresult3) = mycur.fetchone()
f = open(path + './' + filename + './' + a + '.sql', 'w', encoding='utf-8')
f.write(myresult3)
f.close()
j += 1
if str=="视图":
mycur.execute("select TABLE_NAME from information_schema.VIEWS where TABLE_SCHEMA='{}'".format(database))
view=mycur.fetchall()
viewlens=len(view)
j=0
while j<viewlens:
a=(view[j])[0]
mycur.execute("show create table {}".format(a))
(myresult2, myresult3, myresult4, myresult5) = mycur.fetchone()
f = open(path + './' + filename + './' + a + '.sql', 'w', encoding='utf-8')
f.write(myresult3)
f.close()
j+=1
if str=="函数":
mycur.execute("select ROUTINE_NAME from information_schema.ROUTINES where ROUTINE_TYPE ='FUNCTION' and ROUTINE_SCHEMA ='{}'".format(database))
function=mycur.fetchall()
functionlen=len(function)
j=0
while j<functionlen:
a=(function[j])[0]
mycur.execute("show create function {}".format(a))
(myresult2, myresult3, myresult4, myresult5,myresult6,myresult7) = mycur.fetchone()
f = open(path + './' + filename + './' + a + '.sql', 'w', encoding='utf-8')
f.write(myresult4)
f.close()
j+=1
if str=="过程":
mycur.execute("select ROUTINE_NAME from information_schema.ROUTINES where ROUTINE_TYPE ='PROCEDURE' and ROUTINE_SCHEMA ='{}'".format(database))
procedure=mycur.fetchall()
procedurelen=len(procedure)
j=0
while j<procedurelen:
a=(procedure[j])[0]
mycur.execute("show create procedure {}".format(a))
(myresult2, myresult3, myresult4, myresult5,myresult6,myresult7) = mycur.fetchone()
f = open(path + './' + filename + './' + a + '.sql', 'w', encoding='utf-8')
f.write(myresult4)
f.close()
j+=1
if str=="事务":
mycur.execute("select EVENT_NAME from information_schema.EVENTS where EVENT_SCHEMA='{}'".format(database))
event=mycur.fetchall()
eventlen=len(event)
j=0
while j<eventlen:
a=(event[j])[0]
mycur.execute("show create event {}".format(a))
(myresult2, myresult3, myresult4, myresult5,myresult6,myresult7,myresult8) = mycur.fetchone()
f = open(path + './' + filename + './' + a + '.sql', 'w', encoding='utf-8')
f.write(myresult5)
f.close()
j+=1
python-循环获得MySQL所有自建表、视图、函数、过程、事务:
最新推荐文章于 2022-11-02 16:20:19 发布