Semaine 7
This commit is contained in:
BIN
Semaine_07/Jour_03/BDD_Sujet_B.pdf
Normal file
BIN
Semaine_07/Jour_03/BDD_Sujet_B.pdf
Normal file
Binary file not shown.
BIN
Semaine_07/Jour_03/B_Boiché_Gauvain.mwb
Normal file
BIN
Semaine_07/Jour_03/B_Boiché_Gauvain.mwb
Normal file
Binary file not shown.
BIN
Semaine_07/Jour_03/B_Boiché_Gauvain.mwb.bak
Normal file
BIN
Semaine_07/Jour_03/B_Boiché_Gauvain.mwb.bak
Normal file
Binary file not shown.
4
Semaine_07/Jour_03/python/connector/.env
Normal file
4
Semaine_07/Jour_03/python/connector/.env
Normal file
@@ -0,0 +1,4 @@
|
||||
DB_HOST=localhost
|
||||
DB_USER=mysql_user
|
||||
DB_PASSWORD=Azerty123
|
||||
DB_NAME=company
|
||||
1
Semaine_07/Jour_03/python/connector/.python-version
Normal file
1
Semaine_07/Jour_03/python/connector/.python-version
Normal file
@@ -0,0 +1 @@
|
||||
3.12
|
||||
0
Semaine_07/Jour_03/python/connector/README.md
Normal file
0
Semaine_07/Jour_03/python/connector/README.md
Normal file
115
Semaine_07/Jour_03/python/connector/main.py
Normal file
115
Semaine_07/Jour_03/python/connector/main.py
Normal file
@@ -0,0 +1,115 @@
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from mysql.connector import Error, connect, errorcode
|
||||
|
||||
load_dotenv()
|
||||
|
||||
db_host = os.environ.get("DB_HOST")
|
||||
db_user = os.environ.get("DB_USER")
|
||||
db_password = os.environ.get("DB_PASSWORD")
|
||||
db_name = os.environ.get("DB_NAME")
|
||||
|
||||
try:
|
||||
connection = connect(
|
||||
host= db_host,
|
||||
user= db_user,
|
||||
password= db_password,
|
||||
database= db_name
|
||||
)
|
||||
|
||||
cursor = connection.cursor()
|
||||
### Exemple 1 : commande
|
||||
# cursor.execute("SELECT * FROM employee LIMIT 5")
|
||||
# results = cursor.fetchone()
|
||||
# print(results)
|
||||
|
||||
# ### Exemple 2 : requête complète comme variable
|
||||
# query = """
|
||||
# CREATE TABLE IF NOT EXISTS movies (
|
||||
# id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
# title VARCHAR(100),
|
||||
# release_year YEAR(4),
|
||||
# genre VARCHAR(100),
|
||||
# collection_in_mil DECIMAL(4,1)
|
||||
# )
|
||||
# """
|
||||
# cursor.execute(query)
|
||||
# # CIUD : Create, Insert, Update, Delete => Commit
|
||||
# connection.commit()
|
||||
|
||||
### Exemple 3 : multiple requête
|
||||
insert_query = """
|
||||
INSERT INTO movies (title, release_year, genre, collection_in_mil)
|
||||
VALUES
|
||||
("Forrest Gump", 1994, "Drama", 330.2),
|
||||
("INception", 2010, "Aventure", 293.7),
|
||||
("Titanic", 1995, "Drama", 530.4)
|
||||
"""
|
||||
|
||||
create_query = """
|
||||
CREATE TABLE IF NOT EXISTS reviewers (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
firstname VARCHAR(100),
|
||||
lastname VARCHAR(100)
|
||||
)
|
||||
"""
|
||||
|
||||
insert_query_2 = """
|
||||
INSERT INTO reviewers (firstname, lastname)
|
||||
VALUES (%s, %s)
|
||||
"""
|
||||
|
||||
reviewers = [
|
||||
("Jean", "FAIBLE"),
|
||||
("Alain", "TERIEUR"),
|
||||
("Tabatha", "CLOISON"),
|
||||
("Ahmed", "BIEN")
|
||||
]
|
||||
|
||||
cursor.execute(insert_query)
|
||||
cursor.execute(create_query)
|
||||
cursor.executemany(insert_query_2, reviewers)
|
||||
connection.commit()
|
||||
|
||||
cursor.execute("SELECT * FROM movies")
|
||||
# lire un par un
|
||||
resultat = cursor.fetchone()
|
||||
print(resultat)
|
||||
while resultat is not None:
|
||||
print(resultat)
|
||||
resultat = cursor.fetchone()
|
||||
|
||||
# utilisation de lot
|
||||
cursor.execute("SELECT * FROM movies")
|
||||
batch = cursor.fetchmany(size= 4)
|
||||
while batch:
|
||||
print("----")
|
||||
for row in batch:
|
||||
print(row)
|
||||
batch = cursor.fetchmany(4)
|
||||
|
||||
# Out of Memory
|
||||
|
||||
insert_movies_query = """
|
||||
INSERT INTO movies (title, release_year, genre, collection_in_mil)
|
||||
VALUES (%s, %s, %s, %s)
|
||||
"""
|
||||
|
||||
with open("movies.csv", "r") as fichier:
|
||||
reader = csv.reader(fichier)
|
||||
next(reader)
|
||||
|
||||
except Error as error:
|
||||
match error.errno:
|
||||
case errorcode.ER_ACCESS_DENIED_ERROR:
|
||||
print("Accès refusé. Vérifier USER et PASSWORD")
|
||||
case errorcode.ER_BAD_DB_ERROR:
|
||||
print("BDD n'existe pas")
|
||||
case _:
|
||||
print(f"Erreur {error}")
|
||||
finally:
|
||||
if cursor is not None:
|
||||
cursor.close()
|
||||
if connection is not None and connection.is_connected():
|
||||
connection.close()
|
||||
10
Semaine_07/Jour_03/python/connector/pyproject.toml
Normal file
10
Semaine_07/Jour_03/python/connector/pyproject.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[project]
|
||||
name = "connector"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.12"
|
||||
dependencies = [
|
||||
"mysql-connector-python>=9.6.0",
|
||||
"python-dotenv>=1.2.2",
|
||||
]
|
||||
51
Semaine_07/Jour_03/python/connector/uv.lock
generated
Normal file
51
Semaine_07/Jour_03/python/connector/uv.lock
generated
Normal file
@@ -0,0 +1,51 @@
|
||||
version = 1
|
||||
revision = 3
|
||||
requires-python = ">=3.12"
|
||||
|
||||
[[package]]
|
||||
name = "connector"
|
||||
version = "0.1.0"
|
||||
source = { virtual = "." }
|
||||
dependencies = [
|
||||
{ name = "mysql-connector-python" },
|
||||
{ name = "python-dotenv" },
|
||||
]
|
||||
|
||||
[package.metadata]
|
||||
requires-dist = [
|
||||
{ name = "mysql-connector-python", specifier = ">=9.6.0" },
|
||||
{ name = "python-dotenv", specifier = ">=1.2.2" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mysql-connector-python"
|
||||
version = "9.6.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/6f/6e/c89babc7de3df01467d159854414659c885152579903a8220c8db02a3835/mysql_connector_python-9.6.0.tar.gz", hash = "sha256:c453bb55347174d87504b534246fb10c589daf5d057515bf615627198a3c7ef1", size = 12254999, upload-time = "2026-02-10T12:04:52.63Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/8f/d9/2a4b4d90b52f4241f0f71618cd4bd8779dd6d18db8058b0a4dd83ec0541c/mysql_connector_python-9.6.0-cp312-cp312-macosx_14_0_arm64.whl", hash = "sha256:9664e217c72dd6fb700f4c8512af90261f72d2f5d7c00c4e13e4c1e09bfa3d5e", size = 17585672, upload-time = "2026-02-10T12:03:52.955Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/33/91/2495835733a054e716a17dc28404748b33f2dc1da1ae4396fb45574adf40/mysql_connector_python-9.6.0-cp312-cp312-macosx_14_0_x86_64.whl", hash = "sha256:1ed4b5c4761e5333035293e746683890e4ef2e818e515d14023fd80293bc31fa", size = 18452624, upload-time = "2026-02-10T12:03:56.153Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/7a/69/e83abbbbf7f8eed855b5a5ff7285bc0afb1199418ac036c7691edf41e154/mysql_connector_python-9.6.0-cp312-cp312-manylinux_2_28_aarch64.whl", hash = "sha256:5095758dcb89a6bce2379f349da336c268c407129002b595c5dba82ce387e2a5", size = 34169154, upload-time = "2026-02-10T12:03:58.831Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/82/44/67bb61c71f398fbc739d07e8dcadad94e2f655874cb32ae851454066bea0/mysql_connector_python-9.6.0-cp312-cp312-manylinux_2_28_x86_64.whl", hash = "sha256:4ae4e7780fad950a4f267dea5851048d160f5b71314a342cdbf30b154f1c74f7", size = 34542947, upload-time = "2026-02-10T12:04:02.408Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/ba/39/994c4f7e9c59d3ca534a831d18442ac4c529865db20aeaa4fd94e2af5efd/mysql_connector_python-9.6.0-cp312-cp312-win_amd64.whl", hash = "sha256:c180e0b4100d7402e03993bfac5c97d18e01d7ca9d198d742fffc245077f8ffe", size = 16515709, upload-time = "2026-02-10T12:04:04.924Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/2f/58/9521aa678708ec6cebfd40524c14c3d151e4f29e3774e6086aa0a30d203b/mysql_connector_python-9.6.0-cp313-cp313-macosx_14_0_arm64.whl", hash = "sha256:e86e45a7b540ca09af8a18ecfa761e0cdeccfdb62818331614ec030ae44bfd26", size = 17585837, upload-time = "2026-02-10T12:04:07.004Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/39/8d/b108f9bcce9780f6a1f91decb2af54defdaf845e237ddc42f2b4578f1cd7/mysql_connector_python-9.6.0-cp313-cp313-macosx_14_0_x86_64.whl", hash = "sha256:8d3e9252384e1b7f95b07020664f2673d9c29c5e95eeda2e048b3331e190b9d4", size = 18452844, upload-time = "2026-02-10T12:04:09.418Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/d6/28/735cd93d16e76dc2feb4abb3f1229a1d9475af34d80c26712fec6abe1d70/mysql_connector_python-9.6.0-cp313-cp313-manylinux_2_28_aarch64.whl", hash = "sha256:0fa18ead33cb699ea92005695077cef09aa494eebf51164ee30c891c3eaea90c", size = 34169374, upload-time = "2026-02-10T12:04:12.13Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/42/07/069983799cf4050c68f61a494f94b06f095fee6026ab0dd863a14de30867/mysql_connector_python-9.6.0-cp313-cp313-manylinux_2_28_x86_64.whl", hash = "sha256:a26490cb029bf7b18a1d2093101105b3526a1036b51ad01553d30138f5beb8d2", size = 34543019, upload-time = "2026-02-10T12:04:15.065Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/32/00/fbeb7d666ab8153f719e620bac5abfbc74640e8ec511612493110a75fe66/mysql_connector_python-9.6.0-cp313-cp313-win_amd64.whl", hash = "sha256:3460ed976e1b88b7284335d9397a3c519dff56d71580ca1f76ff1c0c7714c813", size = 16515701, upload-time = "2026-02-10T12:04:19.26Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/70/51/13cc90b2a703784cd9a0aa0a6fce07946cf6a2abe7c8fd0b585562e250fc/mysql_connector_python-9.6.0-cp314-cp314-macosx_14_0_arm64.whl", hash = "sha256:e2cc13cd3dcdb845d636e52c4e7a9509b63da09bec6ce1b3696be53a79847e2d", size = 17585800, upload-time = "2026-02-10T12:04:21.6Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/c8/6b/ce7ab998fbdd17f35a1b54624365d039045cbb2d42bbc7b03f50d7597c7b/mysql_connector_python-9.6.0-cp314-cp314-macosx_14_0_x86_64.whl", hash = "sha256:a08c2149d4b52a010c4353f18c84716d18114a4ecd00b466ea34138de2c640f2", size = 18452823, upload-time = "2026-02-10T12:04:23.995Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/f9/bf/8157ed61d17878c33511dcb97c68ecaaaf6220bea5a2944ea4eba73cc63a/mysql_connector_python-9.6.0-cp314-cp314-manylinux_2_28_aarch64.whl", hash = "sha256:b00228b985edd208b20f45c5e684c54e08e31e01bc1d8c3c18a36641c3be5bf7", size = 34171594, upload-time = "2026-02-10T12:04:27.401Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/f7/06/5efdd28819afdb9f1487a62842fda4277febe128a3cd6e9090dbe0a6524e/mysql_connector_python-9.6.0-cp314-cp314-manylinux_2_28_x86_64.whl", hash = "sha256:4617ef5216da7ca32dd46afda61a1552807762434127413bba46fbe4379f59d4", size = 34542851, upload-time = "2026-02-10T12:04:31.021Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/40/6a/26e08a4a79f159cd8e5b64eb10bd056e7735b65d4464d98641f59eb9ca3a/mysql_connector_python-9.6.0-cp314-cp314-win_amd64.whl", hash = "sha256:bc782f64ca00b6b933d4c6a35568f1349d115cc4434c849b5b9edc015bee3e62", size = 17002947, upload-time = "2026-02-10T12:04:34.386Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/15/dd/b3250826c29cee7816de4409a2fe5e469a68b9a89f6bfaa5eed74f05532c/mysql_connector_python-9.6.0-py2.py3-none-any.whl", hash = "sha256:44b0fb57207ebc6ae05b5b21b7968a9ed33b29187fe87b38951bad2a334d75d5", size = 480527, upload-time = "2026-02-10T12:04:36.176Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "python-dotenv"
|
||||
version = "1.2.2"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/82/ed/0301aeeac3e5353ef3d94b6ec08bbcabd04a72018415dcb29e588514bba8/python_dotenv-1.2.2.tar.gz", hash = "sha256:2c371a91fbd7ba082c2c1dc1f8bf89ca22564a087c2c287cd9b662adde799cf3", size = 50135, upload-time = "2026-03-01T16:00:26.196Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/0b/d7/1959b9648791274998a9c3526f6d0ec8fd2233e4d4acce81bbae76b44b2a/python_dotenv-1.2.2-py3-none-any.whl", hash = "sha256:1d8214789a24de455a8b8bd8ae6fe3c6b69a5e3d64aa8a8e5d68e694bbcb285a", size = 22101, upload-time = "2026-03-01T16:00:25.09Z" },
|
||||
]
|
||||
Reference in New Issue
Block a user