-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_db.py
More file actions
175 lines (154 loc) · 4.49 KB
/
python_db.py
File metadata and controls
175 lines (154 loc) · 4.49 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import psycopg2
from psycopg2.extensions import connection
def create_db(conn: connection):
cur = conn.cursor()
cur.execute("""
DROP TABLE IF EXISTS phone_number;
DROP TABLE IF EXISTS users;
""")
conn.commit()
cur.execute("""
CREATE TABLE IF NOT EXISTS users(
id SERIAL PRIMARY KEY,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
email VARCHAR(40) NOT NULL
);
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS phone_number(
id SERIAL PRIMARY KEY,
user_id INTEGER not null references users(id),
number VARCHAR(40) UNIQUE
);
""")
conn.commit()
cur.close()
def add_client(conn: connection,
first_name: str,
last_name: str,
email: str,
phones=None):
cur = conn.cursor()
cur.execute(
"""
INSERT INTO users(first_name,last_name,email) VALUES (%s,%s,%s);
""", (
first_name,
last_name,
email,
))
conn.commit()
cur.execute(
"""
SELECT id FROM users WHERE first_name=%s AND last_name=%s AND email=%s;
""", (first_name, last_name, email))
id = cur.fetchone()
cur.execute("INSERT INTO phone_number(user_id,number) VALUES (%s, %s)",
(id, phones))
cur.close()
def add_phone(conn: connection, client_id, phone):
cur = conn.cursor()
cur.execute("SELECT number FROM phone_number WHERE user_id=%s",
(client_id, ))
value = cur.fetchone()
print(value)
if value == (None, ):
cur.execute(
"""
UPDATE phone_number
SET number=%s
WHERE user_id=%s
""", (phone, client_id))
else:
cur.execute(
"INSERT INTO phone_number(user_id, number) VALUES (%s ,%s)",
(client_id, phone))
conn.commit()
cur.close()
def change_client(conn: connection,
client_id,
first_name=None,
last_name=None,
email=None,
phones=None):
cur = conn.cursor()
# cur.execute("""
# UPDATE users u
# JOIN phone_number pn ON pn.user_id=u.id
# SET first_name=%s, last_name=%s, email=%s, number=%s
# WHERE u.id=%s;
# """, (first_name, last_name, email, phones, client_id,))
cur.execute(
"""
UPDATE users
SET first_name=%s, last_name=%s, email=%s
WHERE id=%s;
""", (first_name, last_name, email, client_id))
cur.execute(
"""
UPDATE phone_number
SET number=%s
WHERE user_id=%s
""", (phones, client_id))
conn.commit()
cur.close()
def delete_phone(conn: connection, client_id, phone):
cur = conn.cursor()
cur.execute(
"""
DELETE FROM phone_number WHERE user_id=%s AND number=%s
""", (client_id, phone))
conn.commit()
cur.close()
def delete_client(conn: connection, client_id):
cur = conn.cursor()
cur.execute(
"""
DELETE FROM phone_number
where user_id=%s;
DELETE FROM users
WHERE id = %s;
""", (client_id, client_id))
conn.commit()
cur.close()
def find_client(conn: connection,
first_name=None,
last_name=None,
email=None,
phone=None):
cur = conn.cursor()
cur.execute(
"""
SELECT u.id,first_name,last_name,email,number FROM users u
JOIN phone_number p ON p.user_id= u.id
GROUP BY u.id,first_name, last_name, email, number
HAVING first_name=%s AND last_name=%s AND email=%s AND number=%s
""", (
first_name,
last_name,
email,
phone,
))
print(cur.fetchone())
cur.close()
with psycopg2.connect(host="89.223.120.167",
port="5432",
database="vkinder_db",
user="postgres",
password="postgres") as conn:
create_db(conn)
add_client(
conn,
'Meow',
'Bark',
"i'mNotCat@gmail.com",
)
add_phone(conn, 1, '+79785105248')
add_client(conn, 'Bark', 'Meow', 'This is some random email',
'88005553535')
add_phone(conn, 2, '1234341234')
change_client(conn, 1, 'Tets', 'Test1', 'Test@email', '88001234')
find_client(conn, 'Tets', 'Test1', 'Test@email', '88001234')
delete_phone(conn, 1, '+79785105248')
delete_client(conn, 2)