Добавление данных в БД

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from sqlite3 import connect, IntegrityError
from tkinter import Tk, StringVar, Label, Entry, Button, messagebox
def add_db(conn, *data):
cursor = conn.cursor()
try:
cursor.execute("insert into addr values (?, ?, ?, ?, ?, ?, ?, ?)", data)
conn.commit()
messagebox.showinfo('OK', 'Данные успешно внесены')
except IntegrityError as e:
messagebox.showinfo("Fail", f'Договор {data[0]} уже занесен в БД')
def execute():
return add_db(
conn,
name_entry.get(),
index_entry.get(),
region_entry.get(),
rajon_entry.get(),
town_entry.get(),
street_entry.get(),
house_entry.get(),
lit_house_entry.get()
)
with connect(r'e:\Python\adress_form\address_dog.db') as conn:
cursor = conn.cursor()
root = Tk()
root.title("Добавить запись в БД")
root.event_add('<<Paste>>', '<Control-igrave>')
root.event_add("<<Copy>>", "<Control-ntilde>")
name_label = Label(text="Договор:")
name_label.grid(row=0, column=0, sticky="w")
name_entry = Entry(textvariable=StringVar())
name_entry.grid(row=0, column=1, padx=5, pady=5)
index_label = Label(text="Индекс:")
index_label.grid(row=1, column=0, sticky="w")
index_entry = Entry(textvariable=StringVar())
index_entry.grid(row=1, column=1, padx=5, pady=5)
region_label = Label(text="Регион/Область:")
region_label.grid(row=2, column=0, sticky="w")
region_entry = Entry(textvariable=StringVar())
region_entry.grid(row=2, column=1, padx=5, pady=5)
rajon_label = Label(text="Район:")
rajon_label.grid(row=3, column=0, sticky="w")
rajon_entry = Entry(textvariable=StringVar())
rajon_entry.grid(row=3, column=1, padx=5, pady=5)
town_label = Label(text="Город/Населенный пункт:")
town_label.grid(row=4, column=0, sticky="w")
town_entry = Entry(textvariable=StringVar())
town_entry.grid(row=4, column=1, padx=5, pady=5)
street_label = Label(text="Улица:")
street_label.grid(row=5, column=0, sticky="w")
street_entry = Entry(textvariable=StringVar())
street_entry.grid(row=5, column=1, padx=5, pady=5)
house_label = Label(text="Дом:")
house_label.grid(row=6, column=0, sticky="w")
house_entry = Entry(textvariable=StringVar())
house_entry.grid(row=6, column=1, padx=5, pady=5)
lit_house_label = Label(text="расширение дома :")
lit_house_label.grid(row=7, column=0, sticky="w")
lit_house_entry = Entry(textvariable=StringVar())
lit_house_entry.grid(row=7, column=1, padx=5, pady=5)
message_button = Button(text="Отправить", command=execute)
message_button.grid(row=8, column=1, padx=5, pady=5, sticky="e")
root.mainloop()