Python operation mysql base example demonstration, including the use of Pymysql, tkinter use
Example 1: Insert data
import pymysql import tkinter as tk conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', Charset ='utf8') master = tk.tk () master.title(" insert vendor info ") 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 (#) hand sql1 = "insert into pro (cName, address, linkman, linkPhone, credit, remark) values(%s,%s,%s,%s,%s,%s)" temp2 = ( ) cur.execute(sql1, Temp2) conn.mit () cur.close() tk.Button(master,text=' insert ',width=8,command=insert). Place (x=140,y=220) master.mainloop() conn.close()Copy the code
Successfully inserted data
Example 2: Get all data for a table
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()
Copy the code
Example 3: Fuzzy search by cName
import pymysql import tkinter as tk conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', Charset ='utf8') # Access database master = tk.tk () master.title(" search for customer information ") 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=' search ', width=8, command=tosearch).pack(padx=20, pady=50) master.mainloop() conn.close()Copy the code
Example 4: Modify data
Identify targets and modify data based on the IDS automatically generated for the data by the database
import pymysql import tkinter as tk conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', Charset ='utf8') master = tk.tk () master.title(" modify vendor info ") 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, 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(): SQL = "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, MIT () cur.close() tk.Button(master,text=' modify ',width=8,command=update). Place (x=140,y=220) master.mainloop() conn.close()Copy the code
Example 5: Delete data
Here, delete by ID
sql1 = "delete from pro where id=%s"
temp1 = str(n)
cur.execute(sql1, temp1)
conn.commit()
cur.close()
Copy the code
The above examples are examples of basic implementation operations. In actual operation, the program and SQL statement can be changed according to requirements to achieve the target effect