Skip to content

Commit 46f94f3

Browse files
authoredMar 26, 2021
Upload source code for Part2 and Part3
1 parent 806fcca commit 46f94f3

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
 

‎python-mysql-azure/db1.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import mysql.connector
2+
3+
# Establish the connection
4+
conn = mysql.connector.connect(
5+
user = '<username>@<server>',
6+
password = '<password>',
7+
host = '<server>.mysql.database.azure.com',
8+
database = '<demodb>'
9+
)
10+
11+
print(conn)

‎python-mysql-azure/db2.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import mysql.connector
2+
3+
# Establish the connection
4+
conn = mysql.connector.connect(
5+
user = '<username>@<server>',
6+
password = '<password>',
7+
host = '<server>.mysql.database.azure.com',
8+
database = '<demodb>'
9+
)
10+
11+
# Create a cursor object using the cursor() method
12+
cursor = conn.cursor()
13+
14+
# Drop previous table of same name if one exists
15+
cursor.execute("DROP TABLE IF EXISTS books")
16+
17+
# Create table
18+
cursor.execute("CREATE TABLE books (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(50), author VARCHAR(50), genre VARCHAR(50))")
19+
print("Finished creating table.")
20+
21+
# Insert some data into table
22+
cursor.execute("INSERT INTO books (title, author, genre) VALUES (%s, %s, %s)", ("East of Eden", "John Steinbeck", "Novel"))
23+
cursor.execute("INSERT INTO books (title, author, genre) VALUES (%s, %s, %s)", ("The Alchemist", "Paulo Coelho", "Novel"))
24+
cursor.execute("INSERT INTO books (title, author, genre) VALUES (%s, %s, %s)", ("The Picture of Dorian Gray", "Oscar Wilde", "Drama"))
25+
cursor.execute("INSERT INTO books (title, author, genre) VALUES (%s, %s, %s)", ("1984", "George Orwell", "Novel"))
26+
27+
# Insert multiple rows
28+
sql = "INSERT INTO books (title, author, genre) VALUES (%s, %s, %s)"
29+
values = [
30+
("The Grapes of Wrath", "John Steinbeck", "Novel"),
31+
("Of Mice and Men", "John Steinbeck", "Novel"),
32+
("The Great Gatsby", "F. Scott Fitzgerald", "Novel"),
33+
("Animal Farm", "George Orwell", "Political satire"),
34+
("The Adventures of Huckleberry Finn", "Mark Twain", "Novel"),
35+
("Little Women", "Louisa May Alcott", "Novel"),
36+
("Hamlet", "William Shakespeare", "Tragedy"),
37+
("The Stranger", "Albert Camus", "Novel"),
38+
("Farmer Giles of Ham", "J. R. R. Tolkien", "Children's literature"),
39+
("Moby Dick", "Herman Melville", "Novel"),
40+
("The Lord of the Rings", "J. R. R. Tolkien", "Fantasy")
41+
]
42+
cursor.executemany(sql, values)
43+
44+
# Read data
45+
cursor.execute("SELECT * FROM books")
46+
rows = cursor.fetchall()
47+
48+
# Print all rows
49+
print("\nBOOKS:\n")
50+
for row in rows:
51+
print(row)
52+
53+
# Update a data row in the table
54+
cursor.execute("UPDATE books SET genre = %s WHERE title = %s", ("Allegorical novella", "Animal Farm"))
55+
56+
# Read data
57+
cursor.execute("SELECT * FROM books")
58+
rows = cursor.fetchall()
59+
60+
# Print all rows
61+
print("\nBOOKS:\n")
62+
for row in rows:
63+
print(row)
64+
65+
# Delete a data row in the table
66+
cursor.execute("DELETE FROM books WHERE title = %s", ("Hamlet", ))
67+
68+
# Delete data rows in the table
69+
cursor.execute("DELETE FROM books WHERE author = %s OR author = %s", ("J. R. R. Tolkien", "Mark Twain"))
70+
71+
# Read data
72+
cursor.execute("SELECT * FROM books")
73+
rows = cursor.fetchall()
74+
75+
# Print all rows
76+
print("\nBOOKS:\n")
77+
for row in rows:
78+
print(row)
79+
80+
# Cleanup
81+
conn.commit()
82+
cursor.close()
83+
conn.close()
84+
print("Done.")

0 commit comments

Comments
 (0)
Please sign in to comment.