-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb2.py
84 lines (68 loc) · 2.73 KB
/
db2.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
import mysql.connector
# Establish the connection
conn = mysql.connector.connect(
user = '<username>@<server>',
password = '<password>',
host = '<server>.mysql.database.azure.com',
database = '<demodb>'
)
# Create a cursor object using the cursor() method
cursor = conn.cursor()
# Drop previous table of same name if one exists
cursor.execute("DROP TABLE IF EXISTS books")
# Create table
cursor.execute("CREATE TABLE books (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(50), author VARCHAR(50), genre VARCHAR(50))")
print("Finished creating table.")
# Insert some data into table
cursor.execute("INSERT INTO books (title, author, genre) VALUES (%s, %s, %s)", ("East of Eden", "John Steinbeck", "Novel"))
cursor.execute("INSERT INTO books (title, author, genre) VALUES (%s, %s, %s)", ("The Alchemist", "Paulo Coelho", "Novel"))
cursor.execute("INSERT INTO books (title, author, genre) VALUES (%s, %s, %s)", ("The Picture of Dorian Gray", "Oscar Wilde", "Drama"))
cursor.execute("INSERT INTO books (title, author, genre) VALUES (%s, %s, %s)", ("1984", "George Orwell", "Novel"))
# Insert multiple rows
sql = "INSERT INTO books (title, author, genre) VALUES (%s, %s, %s)"
values = [
("The Grapes of Wrath", "John Steinbeck", "Novel"),
("Of Mice and Men", "John Steinbeck", "Novel"),
("The Great Gatsby", "F. Scott Fitzgerald", "Novel"),
("Animal Farm", "George Orwell", "Political satire"),
("The Adventures of Huckleberry Finn", "Mark Twain", "Novel"),
("Little Women", "Louisa May Alcott", "Novel"),
("Hamlet", "William Shakespeare", "Tragedy"),
("The Stranger", "Albert Camus", "Novel"),
("Farmer Giles of Ham", "J. R. R. Tolkien", "Children's literature"),
("Moby Dick", "Herman Melville", "Novel"),
("The Lord of the Rings", "J. R. R. Tolkien", "Fantasy")
]
cursor.executemany(sql, values)
# Read data
cursor.execute("SELECT * FROM books")
rows = cursor.fetchall()
# Print all rows
print("\nBOOKS:\n")
for row in rows:
print(row)
# Update a data row in the table
cursor.execute("UPDATE books SET genre = %s WHERE title = %s", ("Allegorical novella", "Animal Farm"))
# Read data
cursor.execute("SELECT * FROM books")
rows = cursor.fetchall()
# Print all rows
print("\nBOOKS:\n")
for row in rows:
print(row)
# Delete a data row in the table
cursor.execute("DELETE FROM books WHERE title = %s", ("Hamlet", ))
# Delete data rows in the table
cursor.execute("DELETE FROM books WHERE author = %s OR author = %s", ("J. R. R. Tolkien", "Mark Twain"))
# Read data
cursor.execute("SELECT * FROM books")
rows = cursor.fetchall()
# Print all rows
print("\nBOOKS:\n")
for row in rows:
print(row)
# Cleanup
conn.commit()
cursor.close()
conn.close()
print("Done.")