美文网首页
实现关键字遍历整个数据库,查找出对应的表和字段-Python

实现关键字遍历整个数据库,查找出对应的表和字段-Python

作者: hao0_0 | 来源:发表于2020-04-26 18:21 被阅读0次

当有这个场景:测试一个业务,需要看数据库对应的表和字段,但只知道业务的某个数据,比如:334.2。那么要查出数据在哪,一般是自己一个表一个表去翻,或者问开发,这样比较浪费时间且不精确。所以我写了一个实现关键字遍历整个数据库搜索出,对应哪个表及字段。

不多说,比较简单,直接上代码:

import pymysql

import warnings

warnings.filterwarnings('ignore')

def export(host,user,passwd,dbname,find):

    conn = pymysql.connect(host,user,passwd,dbname,charset='utf8')

    cursor = conn.cursor()

    cursor.execute('SHOW TABLES')

    DB = cursor.fetchall()

    for i in DB[1:]:

        cursor.execute('select * from %s'%i[0])

        DB_key = cursor.description  # 可以通过此方法在控制台中打印出表中所有的字段名和字段信息

        for j in DB_key:

            try:

                cursor.execute('select %s from %s WHERE %s="%s"'%(j[0],i[0],j[0],find))

            except:pass

            res = cursor.fetchall()

            try:

                a = res[0][0]

                if a == find:

                    print('select %s from %s WHERE %s="%s"' % (j[0], i[0], j[0],find))

                    print("存在->表:%s,字段:%s。值:%s" % (i[0], j[0],a))

            except:pass

    cursor.close()

export('ip', 'username', 'password', 'DB',"一天只能抽一次")  #(ip 服务器地址,用户名,密码,搜索关键字)

效果:

如果帮到你了,点个赞吧。


也可以集成到win窗口,打包成小程序,其他项目的人也可以使用:

import os, sys

from tkinter import *

PythonVersion = 3

from tkinter.font import Font

from tkinter.ttk import *

from tkinter.messagebox import *

import pymysql

#import tkinter.filedialog as tkFileDialog

#import tkinter.simpledialog as tkSimpleDialog    #askstring()

import warnings

warnings.filterwarnings('ignore')

class Application_ui(Frame):

    #这个类仅实现界面生成功能,具体事件处理代码在子类Application中。

    def __init__(self, master=None):

        Frame.__init__(self, master)

        self.master.title('数据库关键字搜索小工具')

        self.master.geometry('520x400')

        self.createWidgets()

    def createWidgets(self):

        self.top = self.winfo_toplevel()

        self.style = Style()

        self.style.configure('搜索.TLabel',anchor='w', background='#C0C0FF', font=('宋体',9))

        self.搜索 = Label(self.top, text='搜索', style='搜索.TLabel')

        self.搜索.place(relx=0.096, rely=0.085, relwidth=0.131, relheight=0.045)

        self.Text1Var = StringVar(value='')

        self.style.configure('Text1.TEntry',foreground='#000000')

        self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体',9), style='Text1.TEntry')

        self.Text1.place(relx=0.257, rely=0.064, relwidth=0.211, relheight=0.088)

        self.style.configure('Label1.TLabel',anchor='w', background='#C0C0FF', font=('宋体',9))

        self.Label1 = Label(self.top, text='数据库', style='Label1.TLabel')

        self.Label1.place(relx=0.514, rely=0.085, relwidth=0.131, relheight=0.045)

        self.Text2Var = StringVar(value='test.yidian.com')

        self.style.configure('Text2.TEntry',foreground='#404040')

        self.Text2 = Entry(self.top, text='test.yidian.com', textvariable=self.Text2Var, font=('宋体',9), style='Text2.TEntry')

        self.Text2.place(relx=0.675, rely=0.064, relwidth=0.243, relheight=0.088)

        self.style.configure('Command1.TButton', background='#FF8080', font=('宋体', 9))

        self.Command1 = Button(self.top, text='搜索', command=self.export, style='Command1.TButton')

        self.Command1.place(relx=0.257, rely=0.233, relwidth=0.179, relheight=0.109)

        self.Text3Font = Font(font=('宋体',9))

        self.Text3 = Text(self.top, bg='#B4B4B4', font=self.Text3Font)

        self.Text3.place(relx=0.032, rely=0.403, relwidth=0.934, relheight=0.576)

        self.Text3.insert("end","日志:搜索时间大概10秒~20秒。点击‘搜索’后,耐心等待~\n-------------------------------------------------------\n")

        self.Text4Var = StringVar(value='test.yidian.com')

        self.Text4 = Entry(self.top, text='test.yidian.com  ', textvariable=self.Text4Var, font=('宋体',9))

        self.Text4.place(relx=0.675, rely=0.191, relwidth=0.243, relheight=0.048)

        self.Text5Var = StringVar(value='test.erp.com')

        self.Text5 = Entry(self.top, text='test.erp.com  ', textvariable=self.Text5Var, font=('宋体',9))

        self.Text5.place(relx=0.675, rely=0.255, relwidth=0.243, relheight=0.048)

        self.Text6Var = StringVar(value='yidian_finance')

        self.Text6 = Entry(self.top, text='yidian_finance', textvariable=self.Text6Var, font=('宋体',9))

        self.Text6.place(relx=0.675, rely=0.318, relwidth=0.243, relheight=0.048)

        self.style.configure('Label1.TLabel',anchor='w', background='#C0C0FF', font=('宋体',9))

        self.Label1 = Label(self.top, text='待选数据库', style='Label1.TLabel')

        self.Label1.place(relx=0.514, rely=0.255, relwidth=0.131, relheight=0.045)

    def export(self):

        dbname=self.Text2Var.get()

        find=self.Text1Var.get()

        conn = pymysql.connect('202.10.', 'dadi', 'password', dbname,charset='utf8')

        cursor = conn.cursor()

        cursor.execute('SHOW TABLES')

        DB = cursor.fetchall()

        for i in DB[1:]:

            cursor.execute('select * from %s'%i[0])

            DB_key = cursor.description  # 可以通过此方法在控制台中打印出表中所有的字段名和字段信息

            for j in DB_key:

                if j[0]=="id":pass

                else:

                    try:

                        cursor.execute('select %s from %s WHERE %s="%s"'%(j[0],i[0],j[0],find))

                    except:pass

                    res = cursor.fetchall()

                    try:

                        a = res[0][0]

                        if a == find:

                            # print('select %s from %s WHERE %s="%s"' % (j[0], i[0], j[0],find))

                            # print("存在->表:%s,字段:%s,值:%s。\n" % (i[0], j[0],a))

                            self.Text3.insert('end',"存在-->表:%s,字段:%s,值:%s \n" % (i[0], j[0],a))

                    except:pass

        cursor.close()

if __name__ == "__main__":

    top = Tk()

    Application_ui(top).mainloop()

    try: top.destroy()

    except: pass

效果:

相关文章

网友评论

      本文标题:实现关键字遍历整个数据库,查找出对应的表和字段-Python

      本文链接:https://www.haomeiwen.com/subject/hupjwhtx.html