侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

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

2023-10-02 星期一 / 0 评论 / 0 点赞 / 52 阅读 / 5373 字

1、django默认支持sqlite3、mysql、oracle、postgresql数据库,像db2和sqlserver之类的数据库需要第三方的支持,具体详见:https://docs.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/mysqlclientMySQLdb的分支,支持python3.3-python3.53、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
#删除pymysqlpip 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__.pyimport pymysqlpymysql.install_as_MySQLdb()

视图view函数

vim polls/views.py
# -*- coding: utf-8 -*-from __future__ import unicode_literalsfrom django.shortcuts import renderfrom django.http import HttpResponse# Create your views here.def index(request):    return HttpResponse("Hello, world. You're at the polls index.")

4、先创建数据库

➜  ~ mysql -uroot -p123456mysql> 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 makemigrationsNo changes detected#执行同步#新版django已经取消sycdb命令同步数据库root># python manage.py migrateSystem 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-modeOperations to perform:  Apply all migrations: admin, auth, contenttypes, sessionsRunning 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

广告 广告

评论区