8000 enregistrement d'une categorie · seriadiallo/python_tkinter_mysql@e0db102 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0db102

Browse files
committed
enregistrement d'une categorie
0 parents  commit e0db102

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode
2+
/venv
3+
__pycache__/

categorie/__init__.py

Whitespace-only changes.

categorie/models.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from connection import CONNECTION
2+
3+
# class Categorie:
4+
5+
# def __init__(self, nom=None):
6+
# self.nom = nom
7+
8+
9+
# def create(self, nom):
10+
11+
# try:
12+
# with connection.cursor() as cursor:
13+
# # Create a new record
14+
# sql = "INSERT INTO `categories` (`nom`) VALUES (%s)"
15+
# cursor.execute(sql, (nom, ))
16+
17+
# # connection is not autocommit by default. So you must commit to save
18+
# # your changes.
19+
# CONNECTION.commit()
20+
21+
22+
# finally:
23+
# CONNECTION.close()
24+
25+
def create(nom):
26+
27+
ligne = 0
28+
try:
29+
with CONNECTION.cursor() as cursor:
30+
# Enregistrer
31+
sql = "INSERT INTO `categories` (`nom`) VALUES (%s)"
32+
ligne = cursor.execute(sql, (nom, ))
33+
34+
except Exception as e:
35+
print('une erreur est survenue')
36+
print(e)
37+
CONNECTION.rollback()
38+
else:
39+
CONNECTION.commit()
40+
finally:
41+
CONNECTION.close()
42+
return ligne

categorie/views.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from tkinter import *
2+
from tkinter.messagebox import showerror, showinfo, showwarning
3+
from tkinter.ttk import *
4+
5+
from .models import create
6+
7+
8+
def formulaire(root):
9+
snom = StringVar()
10+
11+
def save(): # pour la commande
12+
n = snom.get()
13+
if n != None and n != '':
14+
ligne = create(n)
15+
if ligne == 1:
16+
snom.set('')
17+
showinfo('Succes', 'Enregistrement effectué avec succès')
18+
else:
19+
showerror('Erreur', "Une erreur est survenue lors de l'enregistrement")
20+
21+
else:
22+
showwarning("Attention", "Vous devez saisir avant d'enregistrer")
23+
24+
lnom = Label(root, text='Nom')
25+
lnom.grid(row=1, column=0)
26+
27+
28+
nom = Entry(root, textvariable=snom)
29+
nom.grid(row=1, column=1)
30+
31+
button = Button(root, text='Enregistrer', command=save)
32+
button.grid(row=2, column=1)
33+
34+
35+
36+
if __name__ == "__main__":
37+
root = Tk()
38+
root.geometry('700x600')
39+
root.title('Enregistrement')
40+
formulaire(root)
41+
root.mainloop()

connection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pymysql.cursors
2+
# Connect to the database
3+
CONNECTION = pymysql.connect(host='localhost',
4+
user='python',
5+
password='12345678',
6+
db='stock',
7+
charset='utf8mb4',
8+
cursorclass=pymysql.cursors.DictCursor)

database.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
CREATE DATABASE stock;
2+
3+
use stock
4+
5+
CREATE TABLE categories (
6+
id INT auto_increment primary key,
7+
nom VARCHAR(50) NOT NULL
8+
)
9+
ENGINE=INNODB;
10+
11+
12+
CREATE TABLE produits (
13+
id INT auto_increment primary key,
14+
nom VARCHAR(30) NOT NULL,
15+
quantite INT NOT NULL,
16+
date_expiration DATE,
17+
prix INT,
18+
id_categorie INT,
19+
FOREIGN KEY (id_categorie) REFERENCES categories(id)
20+
) ENGINE=INNODB;
21+

main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from tkinter import Tk
2+
3+
from categorie.views import formulaire
4+
5+
if __name__ == "__main__":
6+
root = Tk()
7+
root.geometry('700x600')
8+
root.title('Enregistrement')
9+
formulaire(root)
10+
root.mainloop()

0 commit comments

Comments
 (0)
0