save img in db

This commit is contained in:
vadzik 2023-12-15 17:09:59 +03:00
parent ddce8f7c90
commit ad7754b38b
3 changed files with 28 additions and 18 deletions

BIN
AmDB.db

Binary file not shown.

View File

@ -7,12 +7,12 @@ class Record:
id: int | None = None id: int | None = None
name: str | None = "" name: str | None = ""
description: str | None = "" description: str | None = ""
img_path: str | None = "" img: str | None = ""
def __init__(self, id, name, img_path, description): def __init__(self, id, name, img, description):
self.id = id self.id = id
self.name = name self.name = name
self.img_path = img_path self.img = img
self.description = description self.description = description
@ -29,17 +29,17 @@ class DB:
"""CREATE TABLE IF NOT EXISTS records ( """CREATE TABLE IF NOT EXISTS records (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL, name TEXT NOT NULL,
path TEXT NOT NULL, img BLOB NOT NULL,
description BLOB description TEXT NOT NULL
)""") )""")
self.path = path self.path = path
def add_record(self, name="", description="", img_path=""): def add_record(self, name="", description="", img=""):
self.cur.execute( self.cur.execute(
'''INSERT INTO records (name, path, description) VALUES (?, ?, ?)''', (name, img_path, description)) '''INSERT INTO records (name, img, description) VALUES (?, ?, ?)''', (name, img, description))
self.con.commit() self.con.commit()
def edit_record(self, old_name, name="", img_path="", description=""): def edit_record(self, old_name, name="", img="", description=""):
record = self.get_record_by_name(old_name) record = self.get_record_by_name(old_name)
if record.name != name: if record.name != name:
self.cur.execute( self.cur.execute(
@ -47,9 +47,9 @@ class DB:
if record.description != description: if record.description != description:
self.cur.execute( self.cur.execute(
'UPDATE records SET description = ? WHERE name = ?', (description, old_name)) 'UPDATE records SET description = ? WHERE name = ?', (description, old_name))
if record.img_path != img_path: if record.img != img:
self.cur.execute( self.cur.execute(
'UPDATE records SET path = ? WHERE name = ?', (img_path, old_name)) 'UPDATE records SET img = ? WHERE name = ?', (img, old_name))
self.con.commit() self.con.commit()
def get_record_by_name(self, name): def get_record_by_name(self, name):

26
main.py
View File

@ -1,3 +1,4 @@
import io
from tkinter import * from tkinter import *
from tkinter import ttk, filedialog, messagebox from tkinter import ttk, filedialog, messagebox
import configparser import configparser
@ -67,7 +68,7 @@ def selected(event):
selected_indices = listbox.curselection() selected_indices = listbox.curselection()
selected = ''.join([listbox.get(i) for i in selected_indices]) selected = ''.join([listbox.get(i) for i in selected_indices])
record = database.get_record_by_name(selected) record = database.get_record_by_name(selected)
img = ImageTk.PhotoImage(Image.open(record.img_path)) img = ImageTk.PhotoImage(Image.open(io.BytesIO(record.img)))
image_label.config(image=img) image_label.config(image=img)
desc_label.insert(1.0, record.description) desc_label.insert(1.0, record.description)
@ -82,8 +83,11 @@ def add_record_window(event=""):
image_path_entry.insert(0, path) image_path_entry.insert(0, path)
def add_button(): def add_button():
img = None
with open(image_path_entry.get(), 'rb') as file:
img = file.read()
database.add_record(name=name_entry.get(), description=description_entry.get( database.add_record(name=name_entry.get(), description=description_entry.get(
1.0, "end-1c"), img_path=image_path_entry.get()) 1.0, "end-1c"), img=img)
load_listbox() load_listbox()
add_window.destroy() add_window.destroy()
@ -125,8 +129,15 @@ def update_record_window(event=""):
image_path_entry.insert(0, path) image_path_entry.insert(0, path)
def edit_button(): def edit_button():
img = None
print(image_path_entry.get())
if image_path_entry.get() != "":
with open(image_path_entry.get(), 'rb') as file:
img = file.read()
else:
img = record.img
database.edit_record(selected, name=name_entry.get(), description=description_entry.get( database.edit_record(selected, name=name_entry.get(), description=description_entry.get(
1.0, "end-1c"), img_path=image_path_entry.get()) 1.0, "end-1c"), img=img)
load_listbox() load_listbox()
update_window.destroy() update_window.destroy()
@ -158,7 +169,6 @@ def update_record_window(event=""):
image_path_label = Label(update_window, text="Путь до изображения:") image_path_label = Label(update_window, text="Путь до изображения:")
image_path_label.grid(row=2, column=0) image_path_label.grid(row=2, column=0)
image_path_entry = Entry(update_window, width=50) image_path_entry = Entry(update_window, width=50)
image_path_entry.insert(0, record.img_path)
image_path_entry.grid(row=2, column=1) image_path_entry.grid(row=2, column=1)
browse_button = Button(update_window, text="Обзор", command=browse_image) browse_button = Button(update_window, text="Обзор", command=browse_image)
browse_button.grid(row=2, column=2) browse_button.grid(row=2, column=2)
@ -262,11 +272,11 @@ def main():
fond_menu = Menu(tearoff=0) fond_menu = Menu(tearoff=0)
fond_menu.add_command(label="Найти...") fond_menu.add_command(label="Найти...")
fond_menu.add_separator() fond_menu.add_separator()
fond_menu.add_command(label="Добавить F2", command=add_record_window) fond_menu.add_command(label="Добавить", accelerator="F2", command=add_record_window)
fond_menu.add_command(label="Удалить F3", command=confirm_delete) fond_menu.add_command(label="Удалить", accelerator="F3", command=confirm_delete)
fond_menu.add_command(label="Изменить F4", command=update_record_window) fond_menu.add_command(label="Изменить", accelerator="F4", command=update_record_window)
fond_menu.add_separator() fond_menu.add_separator()
fond_menu.add_command(label="Выход Ctrl+X", command=close_prog) fond_menu.add_command(label="Выход", accelerator="ctrl+X", command=close_prog)
help_menu = Menu(tearoff=0) help_menu = Menu(tearoff=0)
help_menu.add_command(label="Содержание", command=open_help_window) help_menu.add_command(label="Содержание", command=open_help_window)