描述:
⼯作中在数据库中创建表时,当字段很多时,⽐较⿇烦,开发⼀个⼯具,可在excel中写好字段英⽂名和中⽂名,然后通过py⽣成脚本或直接在库中⽣成表脚本:
import tkinter
from tkinter import *
import tkinter.messageboxfrom tkinter import scrolledtextimport xlrd
import pymysqlimport os
#从excel中获取字段项,组合建表语句def get_create_sql(dir_path, file_name): try:
# 打开xls⽂件
#excel_path = \"%s%s\" % (dir_path, file_name) #print(\"excel_path:\
wb = xlrd.open_workbook(filename=dir_path) # 打开第⼀sheet ws = wb.sheets()[0] #获取⾏列数量 nrows = ws.nrows #ncols = ws.ncols #获取sheet1的名称 table_name = ws.name #print(\"nrows:\ #print(\"ncols:\
#print(\"sheet1_name:\ #xlrd中⾏列是从0开始的 for i in range(nrows):
if i == 0: #字段英⽂,格式是列表 row0_data = ws.row_values(0) #print(row0_data)
if i == 1: #字段中⽂,格式是列表 row1_data = ws.row_values(1) #print(row1_data) #将含有-字符的替换
header0 = [str(i).replace(\"-\ #print(\"header0:\
header1 = [str(i).replace(\"-\ #print(\"header1:\ #获取表名
table_commet = file_name.split(\".\")[0] #print(\"table_name:\
create_sql = \"CREATE TABLE `%s` (\\n\" % table_name #print(\"create_sql:\ header0_len = len(header0)
#print(\"header0_len:\ for i in range(header0_len):
#print(\"i:\ if i < header0_len-1:
create_sql += \"`%s` varchar(255) DEFAULT NULL COMMENT '%s',\\n\" % (header0[i], header1[i]) if i == header0_len-1:
create_sql += \"`%s` varchar(255) DEFAULT NULL COMMENT '%s'\\n\" % (header0[i], header1[i]) create_sql += \") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='%s';\\n\" % table_commet #print(\"[INFO]:[create_sql]\ except BaseException as e: #print(\"[ERRO]:\
tkinter.messagebox.showerror('错误', str(e)) return create_sql
def to_db(create_sql,hostname,hostport,username,userpasswd,dbname): #链接数据库,执⾏
#print(hostname,hostport,username,userpasswd,dbname) try:
db = pymysql.connect(host=hostname, port=hostport, user=username, passwd=userpasswd, db=dbname) cursor = db.cursor()
cursor.execute(create_sql) cursor.fetchall() db.close()
#print(\"表创建成功!\")
tkinter.messagebox.showinfo('提⽰', '表创建成功 !') except BaseException as e: #print(\"表创建失败!\
tkinter.messagebox.showerror('错误', str(e))
# get 变量内容def show_db():
if v1.get() == '' or v2.get() == '' or v3.get() == '' or v4.get() == '' or v5.get() == '' or v6.get() == '': tkinter.messagebox.showinfo('提⽰', '请先输⼊内容 !') return
if v1.get() == 'token**123':
tkinter.messagebox.showinfo('提⽰', 'author by lijiyan 2020-12-03,Thank you.') return
#print(\"⽂件路径名 :%s\" % e1.get()) #print(\"数据库IP :%s\" % e2.get()) #print(\"数据库端⼝ :%s\" % e2.get()) #print(\"库名 :%s\" % e2.get()) #print(\"⽤户名 :%s\" % e2.get()) #print(\"密码 :%s\" % e2.get()) #easygui.msgbox('执⾏完成 !') dir_pathname = e1.get()
#dir_pathi = os.path.dirname(dir_pathname) file_namei = os.path.basename(dir_pathname) #print(dir_pathname,file_namei)
create_sql = get_create_sql(dir_path=dir_pathname, file_name=file_namei )
to_db(create_sql=create_sql, hostname=e2.get(), hostport=int(e3.get()), dbname=e4.get(), username=e5.get(), userpasswd=e6.get() )
def show_sql(): if v1.get() == '':
tkinter.messagebox.showinfo('提⽰', '请先输⼊内容 !') return
#print(\"⽂件路径名 :%s\" % e1.get()) dir_pathname = e1.get()
# dir_pathi = os.path.dirname(dir_pathname) file_namei = os.path.basename(dir_pathname) # print(dir_pathname,file_namei)
create_sql = get_create_sql(dir_path=dir_pathname, file_name=file_namei )
#tkinter.messagebox.showinfo('提⽰', create_sql) t.delete(1.0, END)
t.insert('insert', create_sql) #e8.insert('insert',create_sql) #这个可以,但样式难看
#Label(top, text=create_sql,wraplength=200).grid(row=8,column=0,sticky=W+E+N+S,padx=5,pady=5,rowspan=7,columnspan=2)#初始框的声明top = tkinter.Tk()
top.title(\"⽣成createsql⼯具v1.0\")top.geometry(\"400x300+300+200\")
## 标签控件,对Label内容进⾏ 表格式 布局
Label(top,text='⽂件路径名 :').grid(row=0, column=0)Label(top,text='数据库IP :').grid(row=2,column=0)Label(top,text='数据库端⼝ :').grid(row=3,column=0)Label(top,text='库名 :').grid(row=4,column=0)Label(top,text='⽤户名 :').grid(row=5,column=0)Label(top,text='密码 :').grid(row=6,column=0)#Label(top,text='sql :').grid(row=0,column=1)#定义变量
v1 = StringVar()v2 = StringVar()v3 = StringVar()v4 = StringVar()v5 = StringVar()
v6 = StringVar()#v8 = StringVar()
# 输⼊控件,⽤于储存 输⼊的内容e1 = Entry(top, textvariable=v1)e2 = Entry(top, textvariable=v2)e3 = Entry(top, textvariable=v3)e4 = Entry(top, textvariable=v4)e5 = Entry(top, textvariable=v5)
e6 = Entry(top, textvariable=v6, show=\"*\")#e8 = Entry(top, textvariable=v8)
# 进⾏表格式布局
e1.grid(row=0,column=1,padx=10,pady=5)e2.grid(row=2,column=1,padx=10,pady=5)e3.grid(row=3,column=1,padx=10,pady=5)e4.grid(row=4,column=1,padx=10,pady=5)e5.grid(row=5,column=1,padx=10,pady=5)e6.grid(row=6,column=1,padx=10,pady=5)
#e8.grid(row=0,column=2,sticky=W+E+N+S,padx=5,pady=5,rowspan=7,columnspan=2)#⽂本内容较⼤,滚动⽂本框,展⽰⽂本内容
t = scrolledtext.ScrolledText(top,width = 20,height = 20,wrap=WORD)t.grid(row=0,column=2,rowspan=7,columnspan=2)
# 设置 button 指定 宽度 , 并且 关联 函数 , 使⽤表格式布局 .
Button(top,text='⽣成sql',width=10,command=show_sql).grid(row=1,column=1,sticky=E,padx=10,pady=5)Button(top,text='⼊库',width=10,command=show_db).grid(row=7,column=1,sticky=W,padx=10,pady=5)Button(top,text='退出',width=10,command=top.quit).grid(row=7,column=1,sticky=E,padx=10,pady=5)mainloop()
打包⽣成⼯具:在资源栏
excel说明:
第⼀⾏,表字段英⽂名称第⼆⾏,表字段中⽂名称第三⾏,表数据,可不写
到此这篇关于python⾃动⽣成sql语句的脚本的⽂章就介绍到这了,更多相关python⾃动⽣成sql语句内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
因篇幅问题不能全部显示,请点此查看更多更全内容