美文网首页
mysql+models

mysql+models

作者: Tsukinousag | 来源:发表于2022-12-31 16:38 被阅读0次

    cmd启动mysql

    mysql -h localhost -u root -p
    

    1.建立数据库

    显示当前存在的数据库

    删除一个数据库


    2. 创建表格

    使用数据库

    mysql> use mysql_study;
    Database changed
    mysql> create table contacts
        -> (
        -> name char(20) not null,
        -> number char(13) not null
        -> );
    Query OK, 0 rows affected (0.02 sec)
    

    删除数据库

    mysql> drop table mytable
        -> ;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> show tables;
    +-----------------------+
    | Tables_in_mysql_study |
    +-----------------------+
    | contacts              |
    +-----------------------+
    1 row in set (0.00 sec)
    

    表格中增删改查

    mysql> insert into contacts
        -> (name,number)
        -> values
        -> ('tony',15158235102)
        -> ;
    Query OK, 1 row affected (0.01 sec)
    
    mysql> select * from contacts
        -> ;
    +------+-------------+
    | name | number      |
    +------+-------------+
    | tony | 15158235102 |
    +------+-------------+
    1 row in set (0.00 sec)
    
    mysql> insert into contacts
        -> (name,number)
        -> values
        -> ('tom',12345678)
        -> ;
    Query OK, 1 row affected (0.01 sec)
    
    mysql> select * from contacts;
    +------+-------------+
    | name | number      |
    +------+-------------+
    | tony | 15158235102 |
    | tom  | 12345678    |
    +------+-------------+
    2 rows in set (0.00 sec)
    

    删除操作

    mysql> delete from contacts where name="tom";
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from contracts;
    ERROR 1146 (42S02): Table 'mysql_study.contracts' doesn't exist
    mysql> select * from contacts;
    +------+-------------+
    | name | number      |
    +------+-------------+
    | tony | 15158235102 |
    +------+-------------+
    1 row in set (0.00 sec)
    

    修改操作

    查询操作

    mysql> insert into contacts (name,number) values ("dany",13158235102);
    Query OK, 1 row affected (0.01 sec)
    
    mysql> select * from contacts;
    +------+-------------+
    | name | number      |
    +------+-------------+
    | tony | 13912345678 |
    | dany | 13158235102 |
    +------+-------------+
    2 rows in set (0.00 sec)
    
    mysql> select * from contacts where number=13158235102
        -> ;
    +------+-------------+
    | name | number      |
    +------+-------------+
    | dany | 13158235102 |
    +------+-------------+
    1 row in set (0.01 sec)
    

    2. django中MVC设计模式的含义

    把数据存储逻辑,业务逻辑,表现逻辑组合在一起的概念,有时候被称为软件架构的Model-View-Controller(MVC)模式

    • Model 代表数据存储层
    • View 代表的是系统中选择显示什么和怎么显示的部分
    • Controller指的是根据用户输入并需要访问的模型,已决定使用哪个视图的哪个部分

    (django中主要是template,弱化了Controller部分)

    3. django中数据库的配置

    查看是否连接成功

    python manage.py shell
    

    4. 第一个model

    首先配置setting.py函数,在models中建立模型,只有建立好模型才会在数据库中存在

    下面两步骤在数据库mysql_study中建立模型的tabel

    python manage.py makemigrations
    
    python manage.py migrate
    

    在数据库中查看建立好的表


    相关文章

      网友评论

          本文标题:mysql+models

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