Skip to content

Commit 797d54d

Browse files
authored
Upload files for Part 4
1 parent 46f94f3 commit 797d54d

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

python-mysql-azure/books.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
The Alchemist, Paulo Coelho, Novel, 2
2+
Of Mice and Men, John Steinbeck, Novel, 2
3+
The Grapes of Wrath, John Steinbeck, Novel, 4
4+
East of Eden, John Steinbeck, Novel, 3
5+
The Da Vinci Code, Dan Brown, Novel, 2
6+
Life of Pi, Yann Martel, Novel, 3
7+
Hamlet, William Shakespeare, Tragedy, 5
8+
Little Women, Louisa May Alcott, Novel, 1
9+
A Tale of Two Cities, Charles Dickens, Historical novel, 1
10+
The Frogs, Aristophanes, Comedy, 2
11+
Lysistrata, Aristophanes, Comedy, 2
12+
The Universe in a Nutshell, Stephen Hawking, Science, 1
13+
The Design of Everyday Things, Donald A. Norman, Design, 2
14+
The Old Man and the Sea, Ernest Hemingway, Novel, 1
15+
Moby Dick, Herman Melville, Novel, 3
16+
The Adventures of Huckleberry Finn, Mark Twain, Novel, 2
17+
The Great Gatsby, F. Scott Fitzgerald, Novel, 3
18+
The Picture of Dorian Gray, Oscar Wilde, Drama, 4
19+
Travels with Charley: In Search of America, John Steinbeck, Biography, 2
20+
A Christmas Carol, Charles Dickens, Novel, 2
21+
1984, George Orwell, Novel, 5
22+
Animal Farm, George Orwell, Political satire, 4
23+
Anna Karenina, Leo Tolstoy, Novel, 5
24+
The Hobbit, J. R. R. Tolkien, Novel, 3
25+
Farmer Giles of Ham, J. R. R. Tolkien, Children's literature, 1
26+
The Lord of the Rings, J. R. R. Tolkien, Fantasy, 1
27+
Harry Potter and the Philosopher's Stone, J. K. Rowling, Fantasy, 2
28+
Harry Potter and the Prisoner of Azkaban, J. K. Rowling, Fantasy, 1
29+
Harry Potter and the Half-Blood Prince, J. K. Rowling, Fantasy, 1
30+
The Stranger, Albert Camus, Novel, 3

python-mysql-azure/db3.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 library")
16+
17+
# Create a table
18+
cursor.execute("CREATE TABLE library (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(50), author VARCHAR(50), genre VARCHAR(50), quantity INTEGER)")
19+
20+
# Open the file and insert data
21+
fname = 'books.txt'
22+
with open(fname) as fh:
23+
for line in fh:
24+
parts = line.strip().split(", ")
25+
title = parts[0]
26+
author = parts[1]
27+
genre = parts[2]
28+
quantity = parts[3]
29+
30+
# Insert some data into table
31+
cursor.execute("INSERT INTO library (title, author, genre, quantity) VALUES (%s, %s, %s, %s)", (title, author, genre, quantity))
32+
33+
# Read data
34+
cursor.execute("SELECT * FROM library")
35+
rows = cursor.fetchall()
36+
print("Read", cursor.rowcount, "row(s) of data.")
37+
38+
# Print all rows
39+
for row in rows:
40+
print(str(row[0]) + ". " + row[1] + " by " + row[2])
41+
print("Genre: " + row[3] + " Quantity: " + str(row[4]))
42+
43+
# Update quantity
44+
cursor.execute("UPDATE library SET quantity = quantity - 1 WHERE id = %s", (18, ))
45+
print("Updated",cursor.rowcount,"row(s) of data.")
46+
47+
cursor.execute("UPDATE library SET quantity = quantity + 1 WHERE id = %s OR id = %s", (10, 22))
48+
print("Updated",cursor.rowcount,"row(s) of data.")
49+
50+
# Read data
51+
cursor.execute("SELECT * FROM library")
52+
rows = cursor.fetchall()
53+
print("Read", cursor.rowcount, "row(s) of data.")
54+
55+
# Print all rows
56+
for row in rows:
57+
print(str(row[0]) + ". " + row[1] + " by " + row[2])
58+
print("Genre: " + row[3] + " Quantity: " + str(row[4]))
59+
60+
# Cleanup
61+
conn.commit()
62+
cursor.close()
63+
conn.close()
64+
print("Done.")

0 commit comments

Comments
 (0)