-
Notifications
You must be signed in to change notification settings - Fork 1
MySQL Usage Guide
Ozgur Savascioglu edited this page Apr 23, 2025
·
1 revision
This guide explains how to configure MySQL as the database backend in a Django project, and how to perform basic database operations using raw SQL within Django.
Install the MySQL client library:
pip install mysqlclient
If needed:
-
Ubuntu/Debian:
sudo apt-get install python3-dev default-libmysqlclient-dev build-essential
-
macOS:
brew install mysql
In settings.py
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_db_name',
'USER': 'your_mysql_user',
'PASSWORD': 'your_mysql_password',
'HOST': 'localhost',
'PORT': '3306',
}
}
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("""
CREATE TABLE IF NOT EXISTS book (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
author VARCHAR(255),
published_year INT
);
""")
with connection.cursor() as cursor:
cursor.execute("""
INSERT INTO book (title, author, published_year)
VALUES (%s, %s, %s);
""", ["Learning SQL", "Alice", 2023])
with connection.cursor() as cursor:
cursor.execute("""
UPDATE book
SET author = %s
WHERE title = %s;
""", ["Alice Cooper", "Learning SQL"])
with connection.cursor() as cursor:
cursor.execute("""
SELECT title, author, published_year
FROM book
WHERE published_year >= %s;
""", [2022])
rows = cursor.fetchall()
for row in rows:
print(row)
with connection.cursor() as cursor:
cursor.execute("""
DELETE FROM book
WHERE title = %s;
""", ["Learning SQL"])
with connection.cursor() as cursor:
cursor.execute("""
SELECT b.title, a.name
FROM book b
JOIN author a ON b.author = a.id;
""")
results = cursor.fetchall()
from django.db import transaction
try:
with transaction.atomic():
with connection.cursor() as cursor:
cursor.execute("INSERT INTO book (title, author, published_year) VALUES (%s, %s, %s);",
["Transactional Book", "Bob", 2024])
cursor.execute("UPDATE book SET published_year = %s WHERE title = %s;",
[2025, "Transactional Book"])
except Exception as e:
print("Transaction failed:", e)
- Always use parameterized queries (
%s
) to avoid SQL injection. - Use
with connection.cursor()
to ensure proper resource cleanup. - Table and column names are case-sensitive in some MySQL configurations—be consistent.
- You can define SQL scripts in Django migrations using the
RunSQL
operation.
- Celil Özkan (Backend)
- Cem Sarpkaya (Backend)
- Özgür Savaşçıoğlu (Backend)
- Ahmet Hacıoğlu (Frontend)
- Ahmet Selçuk Ersoy (Frontend)
- Bahadır Demirel (Frontend)
- Muhammed Ekinci (Frontend)
- Ali Gökçek (Mobile)
- Seyit Mustafa Demir (Mobile)
- Meeting Agenda 1
- Meeting Agenda 2
- Meeting Agenda 3
- Meeting Agenda 4
- Meeting Agenda 5
- Meeting Agenda 6
- Meeting Notes 1 (16.02.2025)
- Meeting Notes 2 (24.02.2025)
- Meeting Notes 3 (27.02.2025)
- Meeting Notes 4 (04.03.2025)
- Meeting Notes 5 (06.03.2025)
- Meeting Notes 6 (13.03.2025)
- Meeting Notes 7 (17.03.2025)
- Meeting Notes 8 (21.04.2025)
- Meeting Notes 9 (28.04.2025)
- Meeting Notes 10 (01.05.2025)
- Meeting Notes 11 (06.05.2025)
- Lecture Notes 1 (13.02.2025)
- Lecture Notes 2 (20.02.2025)
- Lecture Notes 3 (27.02.2025)
- Lecture Notes 4 (06.03.2025)
- Lecture Notes 5 (13.03.2025)
Click to Expand ⬇️
- Scenario 1 - User Register
- Scenario 2 - Dietitian Register
- Scenario 3 - User Login
- Scenario 4 - User Deletion
- Scenario 5 - Upload & Edit Recipe
- Scenario 6 - Single Meal Planning
- Scenario 7 - Grocery Price Comparison
- Scenario 8 - Community Forum
- Scenario 9 - Recipe Discovery & Filter
- Scenario 10 - Local Food Discovery
- Scenario 11 - Nutritional Guidance Interaction
- Scenario 12 - Market Inventory Management
- Scenario 13 - Profile Management and Preference Settings
- Scenario 14 - Rate and Comment the Recipe by User
- Scenario 15 - Bookmark a Meal and Access it in Profile Page
- Scenario 16 - Adding Nutrition Tips to a Dietitian Profile
- Scenario 17 - User Forget Password
- Scenario 18 ‐ User Follows Unfollows Users
- Scenario 19 - Allergen Alert and Meal Plan Adjustment
- Scenario 20 - Dietitian Rating of a Recipe