数据库操作
www.bilibili.com/video/BV1mj...
1.mysql下载
- mysql +pymysql
django操作数据库更简单,内部提供ORM框架。
pip install mysqlclient
ORM可以做下面2件事:
-
创建、修改、删除表
-
操作表内容
2.创建数据库
手动创建
3.django链接数据库
默认是
python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
mysql的:
python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django42',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': 3306,
}
}
会报错,原因是最新的只支持8.0以上版本的mysql
cmd
(django42) PS C:\Users\jianj\PycharmProjects\djangoProject> python manage.py makemigrations
Traceback (most recent call last):
File "C:\Users\jianj\PycharmProjects\djangoProject\manage.py", line 22, in <module>
.
.
.
.
.
.
File "I:\miniconda3\envs\django42\Lib\site-packages\django\db\backends\base\base.py", line 239, in init_connection_state
self.check_database_version_supported()
File "I:\miniconda3\envs\django42\Lib\site-packages\django\db\backends\base\base.py", line 214, in check_database_version_supported
raise NotSupportedError(
django.db.utils.NotSupportedError: MySQL 8 or later is required (found 5.7.17).
改用最新版mysql成功。
bash
(django42) PS C:\Users\jianj\PycharmProjects\djangoProject> python manage.py makemigrations
Migrations for 'app01':
app01\migrations\0001_initial.py
- Create model UserInfo
cmd
(django42) PS C:\Users\jianj\PycharmProjects\djangoProject> python manage.py migrate
Operations to perform:
Apply all migrations: admin, app01, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
4.增加表
models.py进行修改
ini
from django.db.models import *
# Create your models here.
class UserInfo(Model):
name = CharField(max_length=32)
password = CharField(max_length=64)
age = IntegerField()
class Department(Model):
title=CharField(max_length=16)
class Role(Model):
title=CharField(max_length=16)
再执行
rust
app01_department
app01_role
app01_userinfo
auth_group
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_content_type
django_migrations
django_session
就是修改完毕就得再执行makemigrations 、migrate
修改表新增列需要设置默认值,或者在models.py里直接写 default=xxx
5.操作表数据
python
# 1.增加
UserInfo.objects.create(name="韩梅梅", password="123456", age=18)
UserInfo.objects.create(name="韩梅梅", password="123456", age=18)
UserInfo.objects.create(name="韩梅梅", password="123456", age=18)
Department.objects.create(title="技术部")
Department.objects.create(title="技术部")
Department.objects.create(title="技术部")
Role.objects.create(title="开发工程师")
Role.objects.create(title="开发工程师")
Role.objects.create(title="开发工程师")
# 2.删除
UserInfo.objects.filter(id=3).delete()
UserInfo.objects.all().delete()
# 3.获取数据
datalist = Department.objects.all()
print(datalist)
for obj in datalist:
print(obj.id, obj.name, obj.password, obj.age)
# 4.获取一条数据
obj = Role.objects.filter(id=1).first()
print(obj.id, obj.name, obj.password, obj.age)
# 5.更新数据
Department.objects.all().update(title="开发部")
Department.objects.filter(id=2).update(title="技术部")