model.py 中的代码
 # Create your models here.

 # 书和作者一对多
class Author(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField() class Book(models.Model):
author = models.ForeignKey('Author', on_delete=models.CASCADE)
namebook = models.CharField(max_length=32)
nametype = models.CharField(max_length=32) # 学生和老师多对多 class Teacher(models.Model):
name = models.CharField(max_length=32)
sex = models.CharField(max_length=32) class Student(models.Model):
name = models.CharField(max_length=32)
sex = models.CharField(max_length=32)
teacher = models.ManyToManyField('Teacher')

数据库操作命令:
python manage.py makemigrations
python manage.py migrate setting.py 中的数据库配置
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'as',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '',
}
}
view.py 中的数据库操作
from django.shortcuts import render, HttpResponse
from app01 import models # Create your views here. def add(request):
"""添加Author数据"""
models.Author.objects.create(name='wangjiawei', age=18)
models.Author.objects.create(name='sujiale', age=19)
models.Author.objects.create(name='lixiangshuai', age=20)
models.Author.objects.create(name='zouqingxv', age=21)
return HttpResponse('ok') def test(request):
"""添加Book数据"""
author = models.Author.objects.filter(name='wangjiawei')[0]
models.Book.objects.create(namebook='python', nametype='study', author=author)
author = models.Author.objects.filter(name='zouqingxv')[0]
models.Book.objects.create(namebook='C', nametype='study', author=author)
author = models.Author.objects.filter(name='sujiale')[0]
models.Book.objects.create(namebook='java', nametype='study', author=author)
author = models.Author.objects.filter(name='lixiangshuai')[0]
models.Book.objects.create(namebook='C#', nametype='study', author=author)
author = models.Author.objects.filter(name='wangjiawei')[0]
models.Book.objects.create(namebook='C++', nametype='study', author=author)
author = models.Author.objects.filter(name='wangjiawei')[0]
models.Book.objects.create(namebook='STM32', nametype='study', author=author)
author = models.Author.objects.filter(name='zouqingxv')[0]
models.Book.objects.create(namebook='jQuery', nametype='study', author=author)
author = models.Author.objects.filter(name='sujiale')[0]
models.Book.objects.create(namebook='JavaScript', nametype='study', author=author)
author = models.Author.objects.filter(name='lixiangshuai')[0]
models.Book.objects.create(namebook='树莓派', nametype='study', author=author)
author = models.Author.objects.filter(name='wangjiawei')[0]
models.Book.objects.create(namebook='单片机', nametype='study', author=author) author = models.Author.objects.filter(name='wangjiawei')[0]
models.Book.objects.create(namebook='白雪公主', nametype='play', author=author)
author = models.Author.objects.filter(name='zouqingxv')[0]
models.Book.objects.create(namebook='大力水手', nametype='play', author=author)
author = models.Author.objects.filter(name='sujiale')[0]
models.Book.objects.create(namebook='灰太狼', nametype='play', author=author)
author = models.Author.objects.filter(name='lixiangshuai')[0]
models.Book.objects.create(namebook='水浒传', nametype='play', author=author)
author = models.Author.objects.filter(name='wangjiawei')[0]
models.Book.objects.create(namebook='上下五千年', nametype='play', author=author)
author = models.Author.objects.filter(name='wangjiawei')[0]
models.Book.objects.create(namebook='红猫传', nametype='play', author=author)
author = models.Author.objects.filter(name='zouqingxv')[0]
models.Book.objects.create(namebook='神厨小福贵', nametype='play', author=author)
author = models.Author.objects.filter(name='sujiale')[0]
models.Book.objects.create(namebook='中华小当家', nametype='play', author=author)
author = models.Author.objects.filter(name='lixiangshuai')[0]
models.Book.objects.create(namebook='哆啦A梦', nametype='play', author=author)
author = models.Author.objects.filter(name='wangjiawei')[0]
models.Book.objects.create(namebook='西游记', nametype='play', author=author) return HttpResponse('ok') def select(request):
"""查询"""
# 正向查询
obj = models.Book.objects.filter(namebook='python')[0]
print(obj.author.name)
# 连级查询
obj = models.Book.objects.values('namebook', 'author__name')[0]
print(obj['namebook'] + "++++" + obj['author__name']) # 反向查询
obj = models.Author.objects.filter(name='lixiangshuai')[0]
list = obj.book_set.all().values('namebook')
print(list)
for item in list:
print(item['namebook']) return HttpResponse('ok') def adds(request):
"""添加学生老师"""
models.Teacher.objects.create(name='zhangjiiong', sex='nan') return HttpResponse('ok~!') def adds(request):
"""多对多 添加老师"""
models.Teacher.objects.create(name='wangliangliang', sex='nan')
models.Teacher.objects.create(name='chufengqin', sex='nan')
models.Teacher.objects.create(name='wangli', sex='nv')
models.Teacher.objects.create(name='liuqin', sex='nv')
models.Teacher.objects.create(name='zhouqian', sex='nv') return HttpResponse('ok~!') def addt(request):
"""多对多,添加学生"""
models.Student.objects.create(name='wangjiawei',sex='nan')
models.Student.objects.create(name='gaochengliang', sex='nan')
models.Student.objects.create(name='sujiale', sex='nan')
models.Student.objects.create(name='zouqingxv', sex='nan')
models.Student.objects.create(name='gaoxin', sex='nv')
models.Student.objects.create(name='guanhaiyu', sex='nv') return HttpResponse('ok!!') def stot(request):
"""学生与老师相关联"""
# t1 = models.Teacher.objects.filter(name='wangli')[0]
# print(t1.name)
# s=models.Student.objects.get(name='wangjiawei')
# s.teacher.add(t1)
models.Student.objects.filter(name='sujiale')[0].teacher.add(models.Teacher.objects.get(name='zhangjiong'))
models.Student.objects.filter(name='sujiale')[0].teacher.add(models.Teacher.objects.get(name='wangli'))
models.Student.objects.filter(name='gaoxin')[0].teacher.add(models.Teacher.objects.get(name='wangli'))
models.Student.objects.filter(name='gaoxin')[0].teacher.add(models.Teacher.objects.get(name='zhouqian'))
models.Student.objects.filter(name='guanhaiyu')[0].teacher.add(models.Teacher.objects.get(name='wangliangliang')) # t=models.Teacher.objects.filter(name='chufengqin')[0]
# print(t.name)
return HttpResponse('ok!') def selects(request):
  
  # 反向查询
t= models.Teacher.objects.get(name='zhangjiong')
s=t.student_set.all()
for i in s:
print(i.name)   # 正向查询
s=models.Student.objects.filter(name='wangjiawei')[0]
t=s.teacher.all()
for i in t:
print(i.name)
return HttpResponse('查询完成!')

Python 操作 mysql数据库的一个小小的基础案例,小白新手,以备后用~~的更多相关文章

  1. python操作mysql数据库读取一个数据库的表写入另一个数据库

    写这个肯定是工作需要了,不啰嗦,直接说事 我现在有两台主机,一台是公司主机,一台是客户主机,要求把公司主机上的三个表同步到客户主机上的数据库 注意是同步,首先就得考虑用linux定时任务或者主从复制, ...

  2. Windows下安装MySQLdb, Python操作MySQL数据库的增删改查

    这里的前提是windows上已经安装了MySQL数据库,且配置完成,能正常建表能操作. 在此基础上仅仅需安装MySQL-python-1.2.4b4.win32-py2.7.exe就ok了.仅仅有1M ...

  3. 【转】python操作mysql数据库

    python操作mysql数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库 ...

  4. python接口自动化(三十八)-python操作mysql数据库(详解)

    简介 现在的招聘要求对QA人员的要求越来越高,测试的一些基础知识就不必说了,来说测试知识以外的,会不会一门或者多门开发与语言,能不能读懂代码,会不会Linux,会不会搭建测试系统,会不会常用的数据库, ...

  5. Python 操作MySQL 数据库

    Python 操作 MySQL 数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的 ...

  6. python操作mysql数据库的常用方法使用详解

    python操作mysql数据库 1.环境准备: Linux 安装mysql: apt-get install mysql-server 安装python-mysql模块:apt-get instal ...

  7. python 操作mysql数据库之模拟购物系统登录及购物

    python 操作mysql数据库之模拟购物系统登录及购物,功能包含普通用户.管理员登录,查看商品.购买商品.添加商品,用户充值等. mysql 数据库shop 表结构创建如下: create TAB ...

  8. python + docker, 实现天气数据 从FTP获取以及持久化(二)-- python操作MySQL数据库

    前言 在这一节中,我们主要介绍如何使用python操作MySQL数据库. 准备 MySQL数据库使用的是上一节中的docker容器 “test-mysql”. Python 操作 MySQL 我们使用 ...

  9. Python操作MySQL数据库9个实用实例

    用python连接mysql的时候,需要用的安装版本,源码版本容易有错误提示.下边是打包了32与64版本. MySQL-python-1.2.3.win32-py2.7.exe MySQL-pytho ...

随机推荐

  1. Tensorflow检验GPU是否安装成功 及 使用GPU训练注意事项

    1. 已经安装cuda但是tensorflow仍然使用cpu加速的问题 电脑上同时安装了GPU和CPU版本的TensorFlow,本来想用下面代码测试一下GPU程序,但无奈老是没有调用GPU. imp ...

  2. vue2.0--请求数据

    vue中用vue-reouse请求来的数据,会被封装一层,如下图res:

  3. Amber TUTORIAL 4b: Using Antechamber to Create LEaP Input Files for Simulating Sustiva (efavirenz)-RT complex using the General Amber Force Field (GAFF)

    sustiva.pdb PDB: 1FKO Create parameter and coordinate files for Sustiva 1. 加氢: $ reduce sustiva.pdb ...

  4. iOS UIViewController生命周期控制

    具体流程,看下图: init方法在init方法中实例化必要的对象(遵从LazyLoad思想)init方法中初始化ViewController本身 loadView方法当view需要被展示而它却是nil ...

  5. iOS UI基础-19.0 UICollectionView

    直接上代码,说明请看注释吧 1.继承三个代理 UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateF ...

  6. cocos2d-x C++ (Android)集成第三方微信分享

    ShareSDK Android for cocos2d-x 此文档为ShareSDK Android for cocos3.x的版本,如您集成的是2.X的版本文档请到这里:cocos2.x集成文档 ...

  7. jquery简介未完成

    jQuery jQuery是一个快速.简洁的javascript框架,是继prototype之后又一个优秀的代码库(或者javascript框架).jQuery设计的宗旨是writeLess,Do M ...

  8. webpack配置常用loader加载器

    webapck中使用loader的方法有三种 使用loader之前必须运行安装 : npm install --save-dev xxx-loader (1)通过CLI : 命令行中运行 webpac ...

  9. C#项目中关于多个程序集下App.config文件的问题

    在项目中我们会经常用到App.config文件,有的是自动生成的,比如引用webservice.wcf服务时生成:也有手动建立的配置文件直接默认名就为app.config.这些配置有的保存当前程序集用 ...

  10. 页面每隔n分钟轮换一个微信名和微信名

    1.前端index.html <head> <meta charset="UTF-8"> <title>n号循环</title> & ...