django ORM建立數據庫方法

一、指定鏈接pymysql(python3.x)python

 

先配置_init_.pymysql

import pymysql
pymysql.install_as_MySQLdb()

二、配置鏈接mysql文件信息linux

settings.pysql

DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.mysql', 

        'NAME': 'django_orm',    #你的數據庫名稱

        'USER': 'root',   #你的數據庫用戶名

        'PASSWORD': '', #你的數據庫密碼

        'HOST': '', #你的數據庫主機,留空默認爲localhost

        'PORT': '3306', #你的數據庫端口

    }

}

三、在mysql數據庫中,建立數據庫。數據庫

mysql> create database Django_ORM character set utf8;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| abc                |
| crm                |
| django_orm         |
| mysql              |
| performance_schema |
| s1                 |
| sys                |
| t2                 |
+--------------------+
9 rows in set (0.00 sec)

mysql> use django_orm
Database changed

#####################################3
若是是鏈接linux系統上的mysql數據庫,須要先受權,才能鏈接。
受權語句以下:

  grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;django


  flush privileges;python3.x

四、在app01下面的models.py裏面寫建表語句app

建表語句:ide

class Book(models.Model):
    name=models.CharField(max_length=20)
    price=models.IntegerField()
    pub_date=models.DateField()

 

五、在終端執行命令spa

建立表

python manage.py makemigrations

寫入數據庫

python manage.py migrate
 
六、添加Mysql

配置mysql信息---點OK。

 七、鏈接成功

相關文章
相關標籤/搜索