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. 配置opensips经验总结

    主要参考https://www.cnblogs.com/Forever-Kenlen-Ja/p/7741776.html (ubuntu),还有https://blog.csdn.net/sunyun ...

  2. Python+Selenium框架设计之框架内封装基类和实现POM

    原文地址https://blog.csdn.net/u011541946/article/details/70269965 作者:Anthony_tester 来源:CSDN    博客地址https ...

  3. 《2017年Q2中国城市研究报告

    根据百度慧眼团队发布的<2017年Q2中国城市研究报告>,2017年第二季度人口吸引力排名前五的城市与第一季度相同,深圳继续保持第一的领先位置.

  4. Nodejs中原生遍历文件夹

    最近在听老师讲的node课程,有个关于把异步变为同步读取文件夹的知识点做一些笔记, 让迭代器逐个自执行.

  5. msyql 移动某一列数据到某列 & 字段加前缀

    #移动数据 UPDATE dcs_organize_user AS a, dcs_organize_user AS b SET a.SHORTTELNO=b.USERTELNO WHERE a.id= ...

  6. linux系统状态检测命令

    1.ifconfig命令 ifconfig命令用于获取网卡配置与网络状态等信息,格式为“ifconfig [网络设备] [参数]”. 使用ifconfig命令来查看本机当前的网卡配置与网络状态等信息时 ...

  7. [6]Windows内核情景分析 --APC

    APC:异步过程调用.这是一种常见的技术.前面进程启动的初始过程就是:主线程在内核构造好运行环境后,从KiThreadStartup开始运行,然后调用PspUserThreadStartup,在该线程 ...

  8. 关于ajax原理介绍

    1.ajax技术的背景 不可否认,ajax技术的流行得益于google的大力推广,正是由于google earth.google suggest以及gmail等对ajax技术的广泛应用,催生了ajax ...

  9. 【impala学习之二】impala 使用

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 CM5.4 一.Impala shell 1.进入impal ...

  10. 【Redis学习之十一】Java客户端实现redis集群操作

    客户端:jedis-2.7.2.jar 配置文件两种方式: properties: redis.cluster.nodes1=192.168.1.117 redis.cluster.port1=700 ...