
CREATE TABLE `student_profile` (
`id` int NOT NULL,
`student` varchar(10) NOT NULL,
`profile_photo` longblob NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
COMMIT;
Using SQLite database
CREATE TABLE IF NOT EXISTS student_profile(id integer,
student text,profile_photo blob);This can be linked with our student table and photos of the student can be displayed.
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")my_row=my_conn.execute("SELECT * FROM student_profile limit 0,4")for student in my_row: passHere we used student to collect data. e = Label(my_w, text=student[0]) e.grid(row=i,column=1,ipadx=20) e = Label(my_w, text=student[1]) e.grid(row=i,column=2,ipadx=60) In above code we will add one line images.append(img) to keep refrence to all images in a directory images[]. Without this line only the last image will be displayed. Here the image being used in the Button, or in a Label does not count as a reference for the garbage collector. import iofrom PIL import Image, ImageTk stream = io.BytesIO(student[2]) img=Image.open(stream) img = ImageTk.PhotoImage(img) e = Label(my_w, image=img) e.grid(row=i, column=3,ipady=7) images.append(img) # garbage collection import tkinter as tk from tkinter import * from sqlalchemy import create_engineimport iofrom PIL import Image, ImageTkmy_w = tk.Tk() # parent window my_w.geometry("400x500") # size as width heightmy_w.title("www.plus2net.com") # Adding a title# database connection my_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")my_row=my_conn.execute("SELECT * FROM student_profile limit 0,4")
# Column headers row 0l1=Label(my_w, text='ID') l1.grid(row=0,column=1)
l2=Label(my_w, text='Name') l2.grid(row=0,column=2)
l3=Label(my_w, text='Photo') l3.grid(row=0,column=3)
i=1 # data starts from row 1 images = [] # to manage garbage collection.
for student in my_row: stream = io.BytesIO(student[2]) img=Image.open(stream) img = ImageTk.PhotoImage(img) e = Label(my_w, text=student[0]) e.grid(row=i,column=1,ipadx=20) e = Label(my_w, text=student[1]) e.grid(row=i,column=2,ipadx=60) e = Label(my_w, image=img) e.grid(row=i, column=3,ipady=7) images.append(img) # garbage collection i=i+1 my_w.mainloop()Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
08-08-2023 | |
| id=mycur.execute("INSERT INTO pic(CARNO,NAME,IMG)"/ "VALUES ( %d,%s, %s)",data) TypeError: unsupported operand type(s) for /: 'str' and 'str' | |