Table of contents
1. Basic syntax of python connection to mysql
2. Django framework database operation - ORM framework
2.1 Install third-party modules
3.2 Django connects to the database
3.3 django operation data table
(3) Modify the table structure
1. Basic syntax of python connection to mysql
2. Django framework database operation - ORM framework
ORM can simplify complex code, and translate the code we write into standard SQL statements through the intermediate group of orn, which can greatly reduce our development costs.
2.1 Install third-party modules
pip install mysqlclient
installation failed:
Solution: mysqlclient installation
go to this site for wheels
https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient
Download the corresponding python version
Then copy the files to the project folder:
Go back to the terminal to download:
pip install mysqlclient-1.4.6-cp37-cp37m-win_amd64.whl
Finish:
You can also see it in the settings
3. Use ORM framework
From the above two points, Django can't help us create a database, so we need to create a database ourselves in mysql.
3.1. Create a database
create database djangotest1 default charset=utf8;
3.2 Django connects to the database
Modify in the setting.py file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djangotest1',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
3.3 django operation data table
(1) Create a table
Write the code in app->models.py:
from django.db import models
# Create your models here.
class UserInfo(models.Model):
name = models.CharField(max_length=32)
password = models.CharField(max_length=64)
age = models.IntegerField()
Interpretation:
The classes in models are actually tables created by Django, and Django will translate the python class syntax into SQL statements:
Enter the command in the terminal:
# 迁移数据库
python manage.py makemigrations
python manage.py migrate
If an error is reported
illustrate:
The solution, upgrade mysql
MySQL upgrade 5.6_MySQL upgrade from 5.5 to 5.6_Chen Yiming's blog - CSDN blog
uninstall mysql
https://www.jb51.net/article/231519.htm
https://jingyan.baidu.com/article/1e5468f921400f484961b7d1.html
install mysql
MySQL database installation (detailed) _ experience! Blog - CSDN Blog _mysql installation
MySQL installation (full version)
If there is still an error:
Solve the problem of django.db.utils.OperationalError: (2026, 'SSL connection error: unknown error number') under Windows The version is not suitable or what, it just doesn't work.
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
After some tossing, I found that mysql version 5.5 and version 5.8 are not suitable for Django to use together, why?
Listen to me tell you about blood and tears:
5.5 mysql, Django ignores it,
5.8 mysql, Django can't afford it.
Therefore, it is recommended to use 5.6 and 5.7, because 5.6 does not have ssl, and 5.7 turns off ssl by default.
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
At this point, I can only uninstall and reinstall.
o(╥﹏╥)o
What version of mysql does django support?
Go for a bike and turn it into a motorcycle!
Download 5.7
Install MySQL installation (full version) - Programmer Sought
Query in mysql:
You can see that the table has been created.
The structure is the same as in the class
The other tables are the default tables registered in Django and implement some internal functions:
Create table example:
app->models.py
from django.db import models
# Create your models here.
class UserInfo2(models.Model):
name = models.CharField(max_length=32)
password = models.CharField(max_length=64)
age = models.IntegerField()
class Department(models.Model):
title = models.CharField(max_length=50)
class Employee(models.Model):
position = models.CharField(max_length=50)
Terminal input command:
python manage.py makemigrations
python manage.py migrate
View the database:
It can be observed that our table name is actually: app module name + class name in models
(2) delete table
Directly comment or delete the code of the corresponding class to be deleted
Then execute the command in the terminal
python manage.py makemigrations
python manage.py migrate
View, table has been deleted
(3) Modify the table structure
Delete fields, comments or directly delete the corresponding code, and execute the command in the terminal:
When adding fields, write the corresponding variables first:
class Department(models.Model):
title = models.CharField(max_length=50)
Dep_No = models.IntegerField(default=10)
Dep_Na = models.CharField(max_length=100)
Then enter the command:
python manage.py makemigrations
python manage.py migrate
Then enter the options in the terminal:
1. A one-time default value is now provided (will be set on all existing rows, the value of the column is empty)
Assign values directly in the terminal.
2. Exit, let me add a default value in models.py
After the field add, default = ?
The setting defaults to empty:
Dep_data = models.IntegerField(null=True,blank=True)
Remember, every time the structure of the table is modified, the database migration command is used to reconstruct the data table:
python manage.py makemigrations
python manage.py migrate
Summarize:
: