forked from moctarjallo/torobhe-etoile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtables.py
135 lines (118 loc) · 3.55 KB
/
tables.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python
#https://w3resource.com/PostgreSQL/postgresql-subqueries.php
import psycopg2
from config import config
def sql_creer_table_articles():
return """CREATE TABLE IF NOT EXISTS Articles (
id SERIAL PRIMARY KEY,
nom VARCHAR(255) NOT NULL,
description VARCHAR,
prix REAL
)
"""
def sql_creer_table_clients():
return """ CREATE TABLE IF NOT EXISTS Clients(
id SERIAL PRIMARY KEY,
nom VARCHAR NOT NULL,
addresse VARCHAR(255) NOT NULL,
telephone INT NOT NULL,
email VARCHAR
)
"""
def sql_creer_table_vendeurs():
return """
CREATE TABLE IF NOT EXISTS Vendeurs (
id SERIAL PRIMARY KEY,
nom VARCHAR NOT NULL,
addresse VARCHAR(255) NOT NULL,
telephone INT NOT NULL,
email VARCHAR
)
"""
def sql_creer_table_utilisateurs():
return """
CREATE TABLE IF NOT EXISTS Utilisateurs(
id SERIAL PRIMARY KEY,
nom VARCHAR NOT NULL,
addresse VARCHAR(255),
email VARCHAR NOT NULL,
mot_de_passe text NOT NULL,
telephone INT )"""
def creer_tables():
""" create tables in the PostgreSQL database"""
# Articles
commands = (
sql_creer_table_articles(),
sql_creer_table_clients(),
sql_creer_table_vendeurs(),
sql_creer_table_utilisateurs())
conn = None
try:
# read the connection parameters
params = config()
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
cur = conn.cursor()
# create table one by one
for command in commands:
cur.execute(command)
# commit the changes
conn.commit()
# close communication with the PostgreSQL database server
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print("TABS CREATION DONE SUCCESFULLY")
def supprimer_table(table):
sql = " DROP TABLE {}".format(table)
conn = None
try:
# read the connection parameters
params = config()
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
cur = conn.cursor()
# create table one by one
cur.execute(sql)
# commit the changes
conn.commit()
# close communication with the PostgreSQL database server
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print("TABLE DROP DONE SUCCESFULLY")
def afficher_table(table):
sql = "SELECT * FROM {}".format(table)
conn = None
try:
# read the connection parameters
params = config()
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
cur = conn.cursor()
# create table one by one
cur.execute(sql)
vendeurs = cur.fetchall()
for vendeur in vendeurs:
print(vendeur)
# close communication with the PostgreSQL database server
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def afficher_base_de_donnees():
pass
if __name__ == '__main__':
# supprimer_table("Utilisateurs")
# creer_tables()
afficher_table("Articles")
# afficher_base_de_donnees()
#pass