Skip to content
Snippets Groups Projects
Commit 3646aee8 authored by Françoise Conil's avatar Françoise Conil
Browse files

Création, accès direct et en Python à une base SQLite

parent 8d72a926
No related branches found
No related tags found
No related merge requests found
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import Column, Integer, String
Base = declarative_base()
template = f"""<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Baskets Nike</title>
</head>
<body>
<h1>Liste des modèle Nike disponible</h1>
<h2>Le métier d'ingénieur</h2>
<p>Bonjour</p>
<ol>
<li>BAC L</li>
<li>BAC ES</li>
<li>BAC S</li>
<ol>
<p><img src="protocole-http.png"></p>
</body>
</html>
"""
base = """<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Baskets Nike</title>
</head>
<body>
<h1>Liste des modèle Nike disponibles</h1>"""
fin = """</body>
</html>
"""
class BasketNike(Base):
__tablename__ = 'nike'
id = Column(Integer, primary_key=True)
modele = Column(String)
couleur = Column(String)
image = Column(String)
stock = Column(Integer)
def __repr__(self):
return "{0} : modèle {1}, couleur {2}, stock {3}".format(
self.id,
self.modele,
self.couleur,
self.stock
)
if __name__ == '__main__':
engine = create_engine("sqlite:///basket.db", echo=False)
DBSession = scoped_session(sessionmaker())
DBSession.configure(bind=engine, autoflush=False, expire_on_commit=False)
print(base)
for b in DBSession.query(BasketNike).all():
print(f"""<div>
<div id="modele">{b.modele}</div>
<div id="stock">couleur: {b.couleur}, stock: {b.stock}</div>
<div id="image_basket"><img src="./{b.image}"></div>
</div>
""")
print(fin)
File added
# Créer la base à partir d'instructions SQL
```shell
$ sqlite3 basket.db
SQLite version 3.37.2 2022-01-06 13:25:41
Enter ".help" for usage hints.
sqlite>
```
```sqlite
sqlite> .read cree_basket_bd.sql
sqlite> .tables
nike
sqlite> select * from nike;
1|Nike air force 1|blanche|nike_air_force_1.jpeg|10
2|Nike MK2 Tekno|bleue|nike_mk2_tekno.jpeg|5
3|Nike Air Max Metallic|doré, argenté|nike_air_max_metallic.jpeg|2
```
CREATE TABLE nike (id integer primary key autoincrement, modele text, couleur text, image text, stock integer default 0);
INSERT INTO nike (modele, couleur, image, stock) values ("Nike air force 1", "blanche", "nike_air_force_1.jpeg", 10);
INSERT INTO nike (modele, couleur, image, stock) values ("Nike MK2 Tekno", "bleue", "nike_mk2_tekno.jpeg", 5);
INSERT INTO nike (modele, couleur, image, stock) values ("Nike Air Max Metallic", "doré, argenté", "nike_air_max_metallic.jpeg", 2);
.mode column
.headers on
CREATE TABLE nike (id integer primary key autoincrement, modele text, couleur text, image text, stock integer default 0);
.schema
INSERT INTO nike (modele, couleur, image, stock) values ("Nike air force 1", "blanche", "nike_air_force_1.jpeg", 10);
INSERT INTO nike (modele, couleur, image, stock) values ("Nike MK2 Tekno", "bleue", "nike_mk2_tekno.jpeg", 5);
INSERT INTO nike (modele, couleur, image, stock) values ("Nike Air Max Metallic", "doré, argenté", "nike_air_max_metallic.jpeg", 2);
SELECT * FROM nike;
SELECT * FROM nike where modele like "%air%";
select DISTINCT modele from nike;
select count(*) from nike;
-c requirements.txt
ipython
black
flake8
pytest
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile requirements-dev.in
#
asttokens==2.2.1
# via stack-data
attrs==22.1.0
# via pytest
backcall==0.2.0
# via ipython
black==22.12.0
# via -r requirements-dev.in
click==8.1.3
# via black
decorator==5.1.1
# via ipython
exceptiongroup==1.0.4
# via pytest
executing==1.2.0
# via stack-data
flake8==6.0.0
# via -r requirements-dev.in
iniconfig==1.1.1
# via pytest
ipython==8.7.0
# via -r requirements-dev.in
jedi==0.18.2
# via ipython
matplotlib-inline==0.1.6
# via ipython
mccabe==0.7.0
# via flake8
mypy-extensions==0.4.3
# via black
packaging==22.0
# via
# -c requirements.txt
# pytest
parso==0.8.3
# via jedi
pathspec==0.10.3
# via black
pexpect==4.8.0
# via ipython
pickleshare==0.7.5
# via ipython
platformdirs==2.6.0
# via black
pluggy==1.0.0
# via pytest
prompt-toolkit==3.0.36
# via ipython
ptyprocess==0.7.0
# via pexpect
pure-eval==0.2.2
# via stack-data
pycodestyle==2.10.0
# via flake8
pyflakes==3.0.1
# via flake8
pygments==2.13.0
# via ipython
pytest==7.2.0
# via -r requirements-dev.in
six==1.16.0
# via
# -c requirements.txt
# asttokens
stack-data==0.6.2
# via ipython
tomli==2.0.1
# via
# black
# pytest
traitlets==5.7.1
# via
# ipython
# matplotlib-inline
wcwidth==0.2.5
# via prompt-toolkit
beautifulsoup4
requests
SQLAlchemy
pandas
matplotlib
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile requirements.in
#
beautifulsoup4==4.11.1
# via -r requirements.in
certifi==2022.12.7
# via requests
charset-normalizer==2.1.1
# via requests
contourpy==1.0.6
# via matplotlib
cycler==0.11.0
# via matplotlib
fonttools==4.38.0
# via matplotlib
greenlet==2.0.1
# via sqlalchemy
idna==3.4
# via requests
kiwisolver==1.4.4
# via matplotlib
matplotlib==3.6.2
# via -r requirements.in
numpy==1.23.5
# via
# contourpy
# matplotlib
# pandas
packaging==22.0
# via matplotlib
pandas==1.5.2
# via -r requirements.in
pillow==9.3.0
# via matplotlib
pyparsing==3.0.9
# via matplotlib
python-dateutil==2.8.2
# via
# matplotlib
# pandas
pytz==2022.6
# via pandas
requests==2.28.1
# via -r requirements.in
six==1.16.0
# via python-dateutil
soupsieve==2.3.2.post1
# via beautifulsoup4
sqlalchemy==1.4.45
# via -r requirements.in
urllib3==1.26.13
# via requests
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment