Example output
Dear friends, here are Python and MySQL code examples for the book management system:
import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='root', password='password', db='book_management_system', charset='utf8')
cursor = conn.cursor()
sql_create_table = '''
CREATE TABLE books (
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
author VARCHAR(50) NOT NULL,
publisher VARCHAR(50) NOT NULL,
publish_date DATE NOT NULL,
price DECIMAL(7,2) NOT NULL,
PRIMARY KEY (id)
);
'''
cursor.execute(sql_create_table)
sql_add_book = "INSERT INTO books (title, author, publisher, publish_date, price) VALUES ('Python for Data Analysis', 'Wes McKinney', 'O'Reilly Media', '2017-09-25', 34.99);"
cursor.execute(sql_add_book)
sql_query_book = "SELECT * FROM books WHERE author='Wes McKinney';"
cursor.execute(sql_query_book)
results = cursor.fetchall()
for row in results:
id = row[0]
title = row[1]
author = row[2]
publisher = row[3]
publish_date = row[4]
price = row[5]
print(f"ID:{id}, Title:{title}, Author:{author}, Publisher:{publisher}, Publish Date:{publish_date}, Price:{price}")
cursor.close()
conn.close()