Repository navigation
Expand file tree
/
Copy pathmysql_app.py
More file actions
222 lines (185 loc) · 5.88 KB
/
Copy pathmysql_app.py
File metadata and controls
222 lines (185 loc) · 5.88 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import mysql.connector
from mysql.connector import Error
# Change these connection parameters to match your MySQL server:
HOST_NAME = "localhost"
USER_NAME = "root"
USER_PASSWORD = ""
DB_NAME = "sm_app"
def create_server_connection(host_name, user_name, user_password):
connection = None
try:
connection = mysql.connector.connect(
host=host_name,
user=user_name,
passwd=user_password,
)
print("Connection to MySQL DB successful")
except Error as e:
print(f"The error '{e}' occurred")
return connection
def create_database(connection, query):
cursor = connection.cursor()
try:
cursor.execute(query)
print("Database created successfully")
except Error as e:
print(f"The error '{e}' occurred")
def create_connection(host_name, user_name, user_password, db_name):
connection = None
try:
connection = mysql.connector.connect(
host=host_name,
user=user_name,
passwd=user_password,
database=db_name,
)
print("Connection to MySQL DB successful")
except Error as e:
print(f"The error '{e}' occurred")
return connection
def execute_query(connection, query):
cursor = connection.cursor()
try:
cursor.execute(query)
connection.commit()
print("Query executed successfully")
except Error as e:
print(f"The error '{e}' occurred")
def execute_read_query(connection, query):
cursor = connection.cursor()
result = None
try:
cursor.execute(query)
result = cursor.fetchall()
return result
except Error as e:
print(f"The error '{e}' occurred")
if __name__ == "__main__":
# Connecting to the server and creating the database
connection = create_server_connection(HOST_NAME, USER_NAME, USER_PASSWORD)
create_database_query = "CREATE DATABASE sm_app"
create_database(connection, create_database_query)
connection = create_connection(
HOST_NAME, USER_NAME, USER_PASSWORD, DB_NAME
)
# Creating tables
create_users_table = """
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT,
name TEXT NOT NULL,
age INT,
gender TEXT,
nationality TEXT,
PRIMARY KEY (id)
) ENGINE = InnoDB
"""
execute_query(connection, create_users_table)
create_posts_table = """
CREATE TABLE IF NOT EXISTS posts (
id INT AUTO_INCREMENT,
title TEXT NOT NULL,
description TEXT NOT NULL,
user_id INTEGER NOT NULL,
FOREIGN KEY fk_user_id (user_id) REFERENCES users(id),
PRIMARY KEY (id)
) ENGINE = InnoDB
"""
execute_query(connection, create_posts_table)
# Not shown in the tutorial: the comments and likes tables
create_comments_table = """
CREATE TABLE IF NOT EXISTS comments (
id INT AUTO_INCREMENT,
text TEXT NOT NULL,
user_id INTEGER NOT NULL,
post_id INTEGER NOT NULL,
FOREIGN KEY fk_comment_user_id (user_id) REFERENCES users(id),
FOREIGN KEY fk_comment_post_id (post_id) REFERENCES posts(id),
PRIMARY KEY (id)
) ENGINE = InnoDB
"""
create_likes_table = """
CREATE TABLE IF NOT EXISTS likes (
id INT AUTO_INCREMENT,
user_id INTEGER NOT NULL,
post_id INTEGER NOT NULL,
FOREIGN KEY fk_like_user_id (user_id) REFERENCES users(id),
FOREIGN KEY fk_like_post_id (post_id) REFERENCES posts(id),
PRIMARY KEY (id)
) ENGINE = InnoDB
"""
execute_query(connection, create_comments_table)
execute_query(connection, create_likes_table)
# Inserting records with .execute()
create_users = """
INSERT INTO
`users` (`name`, `age`, `gender`, `nationality`)
VALUES
('James', 25, 'male', 'USA'),
('Leila', 32, 'female', 'France'),
('Brigitte', 35, 'female', 'England'),
('Mike', 40, 'male', 'Denmark'),
('Elizabeth', 21, 'female', 'Canada');
"""
execute_query(connection, create_users)
# Not shown in the tutorial: the posts, comments, and likes records
create_posts = """
INSERT INTO
posts (title, description, user_id)
VALUES
('Happy', 'I am feeling very happy today', 1),
('Hot Weather', 'The weather is very hot today', 2),
('Help', 'I need some help with my work', 2),
('Great News', 'I am getting married', 1),
('Interesting Game', 'It was a fantastic game of tennis', 5),
('Party', 'Anyone up for a late-night party today?', 3);
"""
create_comments = """
INSERT INTO
comments (text, user_id, post_id)
VALUES
('Count me in', 1, 6),
('What sort of help?', 5, 3),
('Congrats buddy', 2, 4),
('I was rooting for Nadal though', 4, 5),
('Help with your thesis?', 2, 3),
('Many congratulations', 5, 4);
"""
create_likes = """
INSERT INTO
likes (user_id, post_id)
VALUES
(1, 6),
(2, 3),
(1, 5),
(5, 4),
(2, 4),
(4, 2),
(3, 6);
"""
execute_query(connection, create_posts)
execute_query(connection, create_comments)
execute_query(connection, create_likes)
# Inserting records with .executemany()
sql = "INSERT INTO likes ( user_id, post_id ) VALUES ( %s, %s )"
val = [(4, 5), (3, 4)]
cursor = connection.cursor()
cursor.executemany(sql, val)
connection.commit()
# Selecting records
select_users = "SELECT * FROM users"
users = execute_read_query(connection, select_users)
for user in users:
print(user)
# Updating table records
update_post_description = """
UPDATE
posts
SET
description = 'The weather has become pleasant now'
WHERE
id = 2
"""
execute_query(connection, update_post_description)
# Deleting table records
delete_comment = "DELETE FROM comments WHERE id = 2"
execute_query(connection, delete_comment)