您好,登錄后才能下訂單哦!
這篇文章主要介紹“python使用pymysql模塊操作MySQL的方法”,在日常操作中,相信很多人在python使用pymysql模塊操作MySQL的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”python使用pymysql模塊操作MySQL的方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
實例一:插入數據
實例二:獲取某個表全部數據
實例三:根據cName模糊搜索
import pymysql import tkinter as tk conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8') master = tk.Tk() master.title("插入供應商信息") master.geometry('350x300') tk.Label(master, text='cName').place(x=30,y=10) tk.Label(master, text='address').place(x=30,y=40) tk.Label(master, text='linkman').place(x=30,y=70) tk.Label(master, text='linkPhone').place(x=30,y=100) tk.Label(master, text='credit').place(x=30,y=130) tk.Label(master, text='remark').place(x=30,y=160) in1=tk.Entry(master, width=30).place(x=100,y=10) in2=tk.Entry(master, width=30).place(x=100,y=40) in3=tk.Entry(master, width=30).place(x=100,y=70) in3=tk.Entry(master, width=30).place(x=100,y=100) in3=tk.Entry(master, width=30).place(x=100,y=130) in3=tk.Entry(master, width=30).place(x=100,y=160) def insert(): cur = conn.cursor() # 伸出手 sql1 = "insert into pro(cName,address,linkman,linkPhone,credit,remark) values(%s,%s,%s,%s,%s,%s)" temp2 = ( ) cur.execute(sql1, temp2) conn.commit() cur.close() tk.Button(master,text='插入',width=8,command=insert).place(x=140,y=220) master.mainloop() conn.close()
成功插入數據
import pymysql conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8') cur = conn.cursor() cur.execute('select * from pro') data = cur.fetchall() cur.close() print(data) conn.close()
import pymysql import tkinter as tk conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8') # 連接數據庫 master = tk.Tk() master.title("搜索某客戶信息") master.geometry('350x300') e = tk.Entry(master) e.pack(padx=20, pady=20) def tosearch(): cur = conn.cursor() temp2 = (e.get(), "%" + e.get() + "%") cur.execute("select * from pro where cName like %s or cName like %s ", temp2) data = cur.fetchall() cur.close() print(data) tk.Button(master, text='搜索', width=8, command=tosearch).pack(padx=20, pady=50) master.mainloop() conn.close()
根據數據庫自動給數據生成的id來確認目標和修改數據
import pymysql import tkinter as tk conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8') master = tk.Tk() master.title("修改供應商信息") master.geometry('350x300') tk.Label(master, text='cName').place(x=30,y=10) tk.Label(master, text='address').place(x=30,y=40) tk.Label(master, text='linkman').place(x=30,y=70) tk.Label(master, text='linkPhone').place(x=30,y=100) tk.Label(master, text='credit').place(x=30,y=130) tk.Label(master, text='remark').place(x=30,y=160) tk.Label(master, text='目標id').place(x=30,y=190) in1=tk.Entry(master, width=30) in1.place(x=100,y=10) in2=tk.Entry(master, width=30) in2.place(x=100,y=40) in3=tk.Entry(master, width=30) in3.place(x=100,y=70) in4=tk.Entry(master, width=30) in4.place(x=100,y=100) in5=tk.Entry(master, width=30) in5.place(x=100,y=130) in6=tk.Entry(master, width=30) in6.place(x=100,y=160) in7=tk.Entry(master, width=30) in7.place(x=100,y=190) def update(): cur = conn.cursor() # 伸出手 sql1 = "update pro set cName=%s, address=%s,linkman=%s,linkPhone=%s,credit=%s,remark=%s where id=%s" temp2 = (in1.get(),in2.get(),in3.get(),in4.get(),in5.get(),in6.get(),in7.get()) cur.execute(sql1, temp2) conn.commit() cur.close() tk.Button(master,text='確認修改',width=8,command=update).place(x=140,y=220) master.mainloop() conn.close()
這里是根據id刪除
sql1 = "delete from pro where id=%s" temp1 = str(n) cur.execute(sql1, temp1) conn.commit() cur.close()
到此,關于“python使用pymysql模塊操作MySQL的方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。