-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPRACTICE_db.py
More file actions
58 lines (52 loc) · 1.91 KB
/
PRACTICE_db.py
File metadata and controls
58 lines (52 loc) · 1.91 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
# CRUD operations in mysql
import mysql.connector
import json
# connecting to db
try:
db = mysql.connector.connect(
host='localhost', user='root', database='sample_library')
print("SUCCESS: Successfully connected to db")
except mysql.connector.Error as error:
print("FAILED: An error occurred while connecting to db:\nError:", error)
# select and return records as json
try:
cursor = db.cursor()
cursor.execute("SELECT * FROM BOOKS")
# type(result)=list
result = cursor.fetchall()
# print as pretty json
print("SUCCESS: records have been fetched successfully:\n",
json.dumps(result, indent=3, sort_keys=True))
except mysql.connector.Error as error:
print("FAILED: An error occurred while getting data:\nError message:",
error.msg, "\nError code:", error.errno)
# update
try:
query = "UPDATE WRITERS SET NAME = 'ASMAA' WHERE ID=1"
cursor.execute(query)
# commit changes
db.commit()
print("SUCCESS: DB is updated successfully")
except mysql.connector.Error as error:
print("FAILED: An error occurred while updating the records:\nError message:",
error.msg, "\nError code:", error.errno)
# insert
try:
query = "INSERT INTO WRITERS (ID, NAME, SURNAME, BIRTHDATE) VALUES (NULL, 'Asmaa', 'Mirkhan', '1985-04-10')"
cursor.execute(query)
# commit changes
db.commit()
print("SUCCESS: record is inserted successfully")
except mysql.connector.Error as error:
print("FAILED: An error occurred while inserting the record:\nError message:",
error.msg, "\nError code:", error.errno)
# delete
try:
query = "DELETE FROM WRITERS WHERE ID = 7"
cursor.execute(query)
# commit changes
db.commit()
print("SUCCESS: record is deleted successfully")
except mysql.connector.Error as error:
print("FAILED: An error occurred while deleting the record:\nError message:",
error.msg, "\nError code:", error.errno)