美文网首页
python mysql命令集合

python mysql命令集合

作者: HopCoder | 来源:发表于2017-09-22 23:58 被阅读0次

    创建模型对象

    # -*- coding:UTF-8 -*-
    from django.db import models
    
    class BookInfoManager(models.Manager):
        # def get_queryset(self):
        #     return super(BookInfoManager,self).get_queryset().filter(isDelete = False)
        def get_queryset(self):
            return super(BookInfoManager,self).get_queryset().filter(isDelete=False)
        def create(self,btitle,bpub_date):
            bookinfo = BookInfo()
            bookinfo.btitle = btitle
            bookinfo.bpub_date = bpub_date
            bookinfo.bread = 0
            bookinfo.bcommet = 0
            bookinfo.isDelete = False
            return bookinfo
    
    class BookInfo(models.Model):
        btitle = models.CharField(max_length=20)
        bpub_date = models.DateTimeField()
        bread = models.IntegerField(default=0,null=True)
        bcommet = models.IntegerField(default=0,null=True)
        isDelete = models.BooleanField(default=False)
    
        books = BookInfoManager()
    
        class Meta():
            ordering = ['id']
            # db_table = ['bookinfo']
            # 定义数据表名称,推荐使用小写字母,数据表的默认名称 <app_name>_<model_name>
    
    
    class HeroInfo(models.Model):
        hname = models.CharField(max_length=20)
        hgender = models.BooleanField(default=True)
        isDelete = models.BooleanField(default=False)
        hcontent = models.CharField(max_length=100,null=True)
        hbook = models.ForeignKey('BookInfo')
    

    1、desc booktest_heroinfo

    image.png

    2、插入数据

    insert into booktest_bookinfo(btitle,bpub_date,bread,bcommet,isDelete) values
    ('射雕英雄传','1980-5-1',12,34,0),
    ('天龙八部','1986-7-24',36,40,0),
    ('笑傲江湖','1995-12-24',20,80,0),
    ('雪山飞狐','1987-11-11',58,24,0)
    

    3、select * from booktest_bookinfo;

    image.png

    4、select * from booktest_bookinfo inner join booktest_heroinfo on booktest_bookinfo.id = hbook_id;

    image.png

    5、删除数据
    delete from df_user_userinfo where uname = 'user01';

    相关文章

      网友评论

          本文标题:python mysql命令集合

          本文链接:https://www.haomeiwen.com/subject/zmuqextx.html