'''main3響應(yīng)函數(shù)'''
def select3(root, label,tree):
sname = label.get()
print('input: ',sname)
# 1.由于剛才已經(jīng)關(guān)閉了數(shù)據(jù)庫(kù)連接,需要重新創(chuàng)建Connection對(duì)象和Cursor對(duì)象
con = pymysql.connect(host='192.168.0.46', user='root', password='root', database='test', charset='utf8', use_unicode=True)
cursor=con.cursor()
print("select * from 配置表匯總 where `站點(diǎn)名稱(局向)` like '"+sname+"'")
c = cursor.execute("select * from 配置表匯總 where `站點(diǎn)名稱(局向)` like '"+sname+"'")
# 2.查詢結(jié)果保存到list_re
list_re=cursor.fetchall()
print('result: ', list_re)
if len(list_re) = 0:
tkinter.messagebox.showinfo('提示',sname+'告警信息不存才!')
else:
print('result_name: ', list_re[0][0])
#數(shù)據(jù)成功提取出來了
# 3.向tree寫入數(shù)據(jù)
for i in range(len(list_re)):
tree.insert('', i, text=i ,values=(list_re[i][0], list_re[i][2],list_re[i][4], list_re[i][14],\
list_re[i][5], list_re[i][9],list_re[i][11], list_re[i][12],\
list_re[i][13]))
tree.grid(column=0,row=1,sticky='NSEW')
con.close()
def insert(root, label1,label2,label3,label4,label5,label6,label7,label8,label9):
net_name = label1.get()
area_name = label2.get()
base_name = label3.get()
belong_name = label4.get()
base_local = label5.get()
base_level = label6.get()
base_num = label7.get()
rural = label8.get()
scene = label9.get()
print('input: ',net_name)
# 由于剛才已經(jīng)關(guān)閉了數(shù)據(jù)庫(kù)連接,需要重新創(chuàng)建Connection對(duì)象和Cursor對(duì)象
con = pymysql.connect(host='192.168.0.46', user='root', password='root', database='test', charset='utf8', use_unicode=True)
cursor=con.cursor()
# SQL 插入語句 里面的數(shù)據(jù)類型要對(duì)應(yīng)
sql = "INSERT INTO 配置表匯總(`網(wǎng)絡(luò)類型`,\
`行政區(qū)`,`基站名稱`,`歸屬區(qū)域`,`基站所處位置具體地址`,\
`基站分級(jí)`,`站點(diǎn)名稱(局向)`,`是否農(nóng)村基站`,`二級(jí)場(chǎng)景類型`)\
VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s')" % \
(net_name,area_name,base_name,belong_name,base_local,base_level,base_num,rural,scene)
print(sql)
try:
# 執(zhí)行sql語句
cursor.execute(sql)
# 執(zhí)行sql語句
con.commit()
tkinter.messagebox.showinfo('提示',net_name+'配置匯總表插入成功!')
except:
# 發(fā)生錯(cuò)誤時(shí)回滾
con.rollback()
con.close()
def delete(root,label7):
base_num = label7.get()
print('input: ',base_num)
# 由于剛才已經(jīng)關(guān)閉了數(shù)據(jù)庫(kù)連接,需要重新創(chuàng)建Connection對(duì)象和Cursor對(duì)象
con = pymysql.connect(host='192.168.0.46', user='root', password='root', database='test', charset='utf8', use_unicode=True)
cursor=con.cursor()
# SQL 插入語句 里面的數(shù)據(jù)類型要對(duì)應(yīng)
sql = "DELETE FROM 配置表匯總 WHERE `站點(diǎn)名稱(局向)`='%s'" %(base_num)
print(sql)
try:
# 執(zhí)行sql語句
cursor.execute(sql)
# 執(zhí)行sql語句
con.commit()
tkinter.messagebox.showinfo('提示',base_num+'配置匯總表刪除成功!')
except:
# 發(fā)生錯(cuò)誤時(shí)回滾
con.rollback()
con.close()
def main3():
monty3 = ttk.LabelFrame(tab3, text='控件示范區(qū)3')
monty3.grid(column=0, row=0,sticky='W', padx=8, pady=4)
monty3_1 = ttk.LabelFrame(tab3, text='控件示范區(qū)2')
monty3_1.grid(column=0, row=1,sticky='W', padx=4, pady=4)
# 查詢結(jié)果
tree=ttk.Treeview(monty3_1)#表格
tree["columns"]=("網(wǎng)絡(luò)類型","行政區(qū)","基站名稱","歸屬區(qū)域","基站所處位置具體地址",\
"基站分級(jí)","站點(diǎn)名稱(局向)","是否農(nóng)村基站","二級(jí)場(chǎng)景類型")
tree.column("網(wǎng)絡(luò)類型",width=50) #表示列,不顯示
tree.column("行政區(qū)",width=50)
tree.column("基站名稱",width=50)
tree.column("歸屬區(qū)域",width=50) #表示列,不顯示
tree.column("基站所處位置具體地址",width=50)
tree.column("基站分級(jí)",width=50)
tree.column("站點(diǎn)名稱(局向)",width=50) #表示列,不顯示
tree.column("是否農(nóng)村基站",width=50)
tree.column("二級(jí)場(chǎng)景類型",width=50)
tree.heading("網(wǎng)絡(luò)類型",text="網(wǎng)絡(luò)類型") #顯示表頭
tree.heading("行政區(qū)",text="行政區(qū)")
tree.heading("基站名稱",text="基站名稱")
tree.heading("歸屬區(qū)域",text="歸屬區(qū)域") #顯示表頭
tree.heading("基站所處位置具體地址",text="基站所處位置具體地址")
tree.heading("基站分級(jí)",text="基站分級(jí)")
tree.heading("站點(diǎn)名稱(局向)",text="站點(diǎn)名稱(局向)") #顯示表頭
tree.heading("是否農(nóng)村基站",text="是否農(nóng)村基站")
tree.heading("二級(jí)場(chǎng)景類型",text="二級(jí)場(chǎng)景類型")
tree.grid(column=0,row=1,sticky='NSEW')
input_name1 = ttk.Label(monty3, text = '網(wǎng)絡(luò)類型:').grid(column=0, row=0, sticky='W',pady=5)
label1 = tkinter.StringVar()
entry1 = tkinter.Entry(monty3,bg='#ffffff',width=20,textvariable=label1).grid(column=1, row=0, sticky='W')
input_name2 = ttk.Label(monty3, text = '行政區(qū):').grid(column=3, row=0, sticky='W')
label2 = tkinter.StringVar()
entry2 = tkinter.Entry(monty3,bg='#ffffff',width=20,textvariable=label2).grid(column=4, row=0, sticky='W')
input_name3 = ttk.Label(monty3, text = '基站名稱:').grid(column=0, row=1, sticky='W',pady=5)
label3 = tkinter.StringVar()
entry3 = tkinter.Entry(monty3,bg='#ffffff',width=20,textvariable=label3).grid(column=1, row=1, sticky='W')
input_name4 = ttk.Label(monty3, text = '歸屬區(qū)域:').grid(column=3, row=1, sticky='W')
label4 = tkinter.StringVar()
entry4 = tkinter.Entry(monty3,bg='#ffffff',width=20,textvariable=label4).grid(column=4, row=1, sticky='W')
input_name5 = ttk.Label(monty3, text = '基站所處位置具體地址:').grid(column=0, row=2, sticky='W',pady=5)
label5 = tkinter.StringVar()
entry5 = tkinter.Entry(monty3,bg='#ffffff',width=20,textvariable=label5).grid(column=1, row=2, sticky='W')
input_name6 = ttk.Label(monty3, text = '基站分級(jí):').grid(column=0, row=3, sticky='W',pady=5)
label6 = tkinter.StringVar()
entry6 = tkinter.Entry(monty3,bg='#ffffff',width=20,textvariable=label6).grid(column=1, row=3, sticky='W')
input_name7 = ttk.Label(monty3, text = '站點(diǎn)名稱(局向):').grid(column=0, row=4, sticky='W',pady=5)
label7 = tkinter.StringVar()
entry7 = tkinter.Entry(monty3,bg='#ffffff',width=20,textvariable=label7).grid(column=1, row=4, sticky='W')
input_name8 = ttk.Label(monty3, text = '是否農(nóng)村基站:').grid(column=0, row=5, sticky='W',pady=5)
label8 = tkinter.StringVar()
entry8 = tkinter.Entry(monty3,bg='#ffffff',width=20,textvariable=label8).grid(column=1, row=5, sticky='W')
input_name9 = ttk.Label(monty3, text = '二級(jí)場(chǎng)景類型:').grid(column=0, row=6, sticky='W',pady=5)
label9 = tkinter.StringVar()
entry9 = tkinter.Entry(monty3,bg='#ffffff',width=20,textvariable=label9).grid(column=1, row=6, sticky='W')
select_button = tkinter.Button(monty3,bg='white',text='查詢',width=10,height=1,\
command=lambda :select3(monty3, label7,tree)).grid(column=0, row=7, sticky='W',pady=5)
insert_button = tkinter.Button(monty3,bg='white',text='插入',width=10,height=1,\
command=lambda :insert(monty3,label1,label2,label3,label4,label5,label6,\
label7,label8,label9)).grid(column=1, row=7, sticky='W',padx=5,pady=5)
delete_button = tkinter.Button(monty3,bg='white',text='刪除',width=10,height=1,\
command=lambda :delete(monty3, label7)).grid(column=2, row=7, sticky='W',pady=5)
到此這篇關(guān)于Python中tkinter+MySQL實(shí)現(xiàn)增刪改查的文章就介紹到這了,更多相關(guān)tkinter MySQL增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!