python-循环获得MySQL所有自建表、视图、函数、过程、事务:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值