2023-01-21: mySQL and Python#
mySQL and Python#
To install MySQL on Windows and write Python code in Visual Studio Code (VSCode) with MySQL integration, follow these steps:
1. Install MySQL#
a. MySQL Installer:#
Visit the MySQL Downloads page.
Download the MySQL Installer for Windows.
Run the installer and follow the on-screen instructions.
During installation, select “MySQL Server” and “MySQL Workbench” components.
b. MySQL Server Configuration:#
During installation, you’ll be prompted to configure the MySQL Server.
Set a root password and remember it.
Configure other settings as needed.
c. MySQL Workbench:#
After installing, open MySQL Workbench.
Connect to the MySQL Server using the root password.
2. Install Python and MySQL Connector#
a. Install Python:#
Download and install Python from python.org.
During installation, make sure to check the option to add Python to the system PATH.
b. Install MySQL Connector:#
Open a command prompt or PowerShell and run:
pip install mysql-connector-python
3. Set Up Visual Studio Code#
a. Install VSCode:#
Download and install Visual Studio Code from here.
b. Install Python Extension:#
Open VSCode.
Go to Extensions (Ctrl+Shift+X).
Search for “Python” and install the official Python extension.
4. Write Python Code in VSCode#
a. Create a Python File:#
Open VSCode.
Create a new Python file with a
.pyextension.
b. Write Python Code with MySQL Connector:#
import mysql.connector
# Connect to MySQL Server
conn = mysql.connector.connect(
host="localhost",
user="root",
password="your_password",
database="your_database"
)
# Create a cursor
cursor = conn.cursor()
# Example Query
query = "SELECT * FROM your_table"
cursor.execute(query)
# Fetch and print results
for row in cursor.fetchall():
print(row)
# Close the cursor and connection
cursor.close()
conn.close()
Replace "your_password", "your_database", and "your_table" with your MySQL password, database name, and table name.
c. Run the Python Code:#
Save the Python file.
Open a terminal in VSCode.
Run the script using
python your_file.py.
This script connects to your MySQL database, executes a query, and prints the results.
Remember to adapt the code according to your specific database and table names.