2、Django数据库模块配置和管理

1、django默认支持sqlite3、mysql、oracle、postgresql数据库,像db2和sqlserver之类的数据库需要第三方的支持,具体详见:
https://docs.djangoproject.com/en/1.11/ref/databases/

输入图片说明

https://docs.djangoproject.com/en/1.11/ref/databases/#using-a-3rd-party-database-backend

###一、sqlite3### django默认使用sqlite的数据库,默认自带sqlite的数据库驱动 引擎名称:

django.db.backends.sqlite3

###二、mysql驱动程序###

1、Mysqldb(mysql-python):  https://pypi.python.org/pypi/MySQL-python/1.2.5
比较经典,但MySQLdb只支持Python2.*,还不支持3.*

2、mysqlclient:  https://pypi.python.org/pypi/mysqlclient
MySQLdb的分支,支持python3.3-python3.5

3、MySQL Connector/Python:  https://dev.mysql.com/downloads/connector/python/
MySQL官方的一个驱动

4、PyMySQL:https://pypi.python.org/pypi/PyMySQL
纯python的mysql驱动,选择这个的理由为,其他驱动对系统依赖比较高,需要c++什么的,所以选择这个,可以直接用pip install pymysql安装

###三、演示pymysql的使用### 1、安装pymysql

pip install pymysql
#删除pymysql
pip uninstall pymysql

2、修改配置文件

vim mysite/settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'aud2',
        'USER': 'aud2',
        'PASSWORD': 'aud2!@#',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

3、在工程目录的__init__.py添加代码,不然会报错,Django默认使用的是Mysqldb模块,我们使用的pymsyql,需要调整下代码:

报错代码

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb.
Did you install mysqlclient or MySQL-python?

解决办法

###在工程目录中的__init__.py中添加如下代码
vim mysite/__init__.py

import pymysql
pymysql.install_as_MySQLdb()

视图view函数

vim polls/views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

4、先创建数据库

➜  ~ mysql -uroot -p123456

mysql> create database mysite;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on *.* to mysite@"localhost" identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to mysite@"%" identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for mysite@"%";
+----------------------------------------------------------------------------------------------------------------+
| Grants for mysite@%                                                                                            |
+----------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'mysite'@'%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

5、同步数据操作

#生成数据的脚本
python manage.py makemigrations
No changes detected

#执行同步
#新版django已经取消sycdb命令同步数据库
root># python manage.py migrate
System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
	HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK

6、启动服务

python manage.py runserver 10.118.166.110:5000

7、访问测试

输入图片说明

参考文档:

import MySQLdb与import mysql区别

https://zhidao.baidu.com/question/1834288931305871860.html

转载于:https://my.oschina.net/u/3138954/blog/1476562

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值