How to use MySQL and Python to develop a simple online music platform
With the rapid development of the Internet, music platforms have become indispensable in many people’s entertainment lives part. This article will introduce how to use MySQL and Python to develop a simple online music platform.
1. Preparation
First, we need to install MySQL and Python on the computer. MySQL is a commonly used relational database management system, and Python is a simple and easy-to-use programming language.
Install MySQL:
Install Python:
2. Create a database
Open MySQL and log in using the username and password set previously. Create a new database to store data related to music platforms.
Sample code:
CREATE DATABASE music_platform;
3. Create a data table
Create a data table in the database to store related information such as songs, artists, and users.
Sample code:
USE music_platform; CREATE TABLE songs ( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(100), artist VARCHAR(100), duration INT ); CREATE TABLE artists ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), genre VARCHAR(50) ); CREATE TABLE users ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50), password VARCHAR(50) );
4. Connect to the database
Use the pymysql
library in Python to connect to the MySQL database and establish the corresponding table structure.
Sample code:
import pymysql # 连接到MySQL数据库 conn = pymysql.connect( host='localhost', # MySQL主机名 user='your_username', # MySQL用户名 password='your_password', # MySQL密码 database='music_platform' # 数据库名 ) # 创建数据表 def create_tables(): cursor = conn.cursor() cursor.execute(''' CREATE TABLE IF NOT EXISTS songs ( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(100), artist VARCHAR(100), duration INT ); ''') cursor.execute(''' CREATE TABLE IF NOT EXISTS artists ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), genre VARCHAR(50) ); ''') cursor.execute(''' CREATE TABLE IF NOT EXISTS users ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50), password VARCHAR(50) ); ''') conn.commit() # 关闭数据库连接 def close_connection(): conn.close()
5. Operation database
After creating the database and data table, we can add, delete, modify and query data through Python code.
Add song:
def add_song(title, artist, duration): cursor = conn.cursor() cursor.execute('INSERT INTO songs (title, artist, duration) VALUES (%s, %s, %s)', (title, artist, duration)) conn.commit()
Add artist:
def add_artist(name, genre): cursor = conn.cursor() cursor.execute('INSERT INTO artists (name, genre) VALUES (%s, %s)', (name, genre)) conn.commit()
Add user:
def add_user(username, password): cursor = conn.cursor() cursor.execute('INSERT INTO users (username, password) VALUES (%s, %s)', (username, password)) conn.commit()
Query song:
def search_songs(title): cursor = conn.cursor() cursor.execute('SELECT * FROM songs WHERE title = %s', (title,)) return cursor.fetchall()
Query artist:
def search_artists(name): cursor = conn.cursor() cursor.execute('SELECT * FROM artists WHERE name = %s', (name,)) return cursor.fetchall()
Query user:
def search_users(username): cursor = conn.cursor() cursor.execute('SELECT * FROM users WHERE username = %s', (username,)) return cursor.fetchall()
The above is only a sample code. In actual development, it needs to be optimized and improved according to business needs.
6. Summary
This article introduces how to use MySQL and Python to develop a simple online music platform. By creating databases and data tables, and using Python code to operate the database, we have implemented the functions of adding, deleting, modifying, and querying data such as songs, artists, and users. Of course, in the actual development process, the code needs to be further optimized and improved to make the music platform more complete and stable. I hope this article can help you develop a music platform.
The above is the detailed content of How to develop a simple online music platform using MySQL and Python. For more information, please follow other related articles on the PHP Chinese website!