Skip to content

Commit f11a0a6

Browse files
committed
added using mysql tutorial
1 parent db0b45c commit f11a0a6

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
100100
- [How to Use Google Drive API in Python](https://www.thepythoncode.com/article/using-google-drive--api-in-python). ([code](general/using-google-drive-api))
101101
- [How to Translate Text in Python](https://www.thepythoncode.com/article/translate-text-in-python). ([code](general/using-google-translate-api))
102102

103+
- ### Database
104+
- [How to Use MySQL Database in Python](https://www.thepythoncode.com/article/using-mysql-database-in-python). ([code](database/mysql-connector))
105+
103106
For any feedback, please consider pulling requests.

database/mysql-connector/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# [How to Use MySQL Database in Python](https://www.thepythoncode.com/article/using-mysql-database-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mysql-connector-python
2+
tabulate
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import mysql.connector as mysql
2+
from tabulate import tabulate
3+
4+
# insert MySQL Database information here
5+
HOST = "localhost"
6+
DATABASE = ""
7+
USER = "root"
8+
PASSWORD = ""
9+
10+
# connect to the database
11+
db_connection = mysql.connect(host=HOST, database=DATABASE, user=USER, password=PASSWORD)
12+
# get server information
13+
print(db_connection.get_server_info())
14+
# get the db cursor
15+
cursor = db_connection.cursor()
16+
# get database information
17+
cursor.execute("select database();")
18+
database_name = cursor.fetchone()
19+
print("[+] You are connected to the database:", database_name)
20+
# create a new database called library
21+
cursor.execute("create database if not exists library")
22+
23+
# use that database
24+
cursor.execute("use library")
25+
print("[+] Changed to `library` database")
26+
# create a table
27+
cursor.execute("""create table if not exists book (
28+
`id` integer primary key auto_increment not null,
29+
`name` varchar(255) not null,
30+
`author` varchar(255) not null,
31+
`price` float not null,
32+
`url` varchar(255)
33+
)""")
34+
print("[+] Table `book` created")
35+
36+
# insert some books
37+
books = [
38+
{
39+
"name": "Automate the Boring Stuff with Python: Practical Programming for Total Beginners",
40+
"author": "Al Sweigart",
41+
"price": 17.76,
42+
"url": "https://amzn.to/2YAncdY"
43+
},
44+
{
45+
"name": "Python Crash Course: A Hands-On, Project-Based Introduction to Programming",
46+
"author": "Eric Matthes",
47+
"price": 22.97,
48+
"url": "https://amzn.to/2yQfQZl"
49+
},
50+
{
51+
"name": "MySQL for Python",
52+
"author": "Albert Lukaszewski",
53+
"price": 49.99,
54+
}
55+
]
56+
57+
# iterate over books list
58+
for book in books:
59+
id = book.get("id")
60+
name = book.get("name")
61+
author = book.get("author")
62+
price = book.get("price")
63+
url = book.get("url")
64+
# insert each book as a row in MySQL
65+
cursor.execute("""insert into book (id, name, author, price, url) values (
66+
%s, %s, %s, %s, %s
67+
)
68+
""", params=(id, name, author, price, url))
69+
print(f"[+] Inserted the book: {name}")
70+
71+
# commit insertion
72+
db_connection.commit()
73+
74+
# fetch the database
75+
cursor.execute("select * from book")
76+
# get all selected rows
77+
rows = cursor.fetchall()
78+
# print all rows in a tabular format
79+
print(tabulate(rows, headers=cursor.column_names))
80+
# close the cursor
81+
cursor.close()
82+
# close the DB connection
83+
db_connection.close()

0 commit comments

Comments
 (0)