首页 / 更多教程 / django下数据库配置
django下数据库配置
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了django下数据库配置,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3893字,纯文字阅读大概需要6分钟。
内容图文
![django下数据库配置](/upload/InfoBanner/zyjiaocheng/499/15ff9c2712bc4a34a21a2274aad20e44.jpg)
1、django默认支持sqlite、mysql、Oracle、postgresql数据库,像db2和sqlserver之类的数据库之类的数据库支持需要第三方的支持。具体详见:
https://docs.djangoproject.com/en/1.9/ref/databases/
Using a 3rd-party database backend?
In addition to the officially supported databases, there are backends provided by 3rd parties that allow you to use other databases with Django:
SAP SQL Anywhere
IBM DB2
Microsoft SQL Server
Firebird
ODBC
The Django versions and ORM features supported by these unofficial backends vary considerably. Queries regarding the specific capabilities of these unofficial backends, along with any support queries, should be directed to the support channels provided by each 3rd party project.
sqlite:
django默认使用sqlite的数据库,默认自带sqlite的数据库驱动,
引擎名称:
django.db.backends.sqlite3
mysql
引擎名称:
django.db.backends.mysql
2、MySQL驱动引擎下载地址
MySQLdb(mysql-python):https://pypi.python.org/pypi/MySQL-python/1.2.5,仅仅支持Python2,Python2以后就没有更新,所以不支持Python3
mysqlclient:https://pypi.python.org/pypi/mysqlclient,是mysqldb的一个分支,支持Python3
MySQL官方驱动
Connectoer/Python: https://dev.mysql.com/downloads/connectoter/python
针对各个版本的Python都有特定的支持包
PyMySQL(纯python的mysql驱动):
http://pypyi.python.org/pypi/PyMySQL
本次课程的Python驱动采用该驱动支持
3、演示pymysql的使用
1、安装pymysql
2、修改配置文件
错误提示:
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named ‘MySQLdb‘
django用mysql数据库时,默认数据库引擎时导入mysqldb模块,所以为了不出现修改默认引擎,可以做如下操作:
import pymysql
pymysql.install_as_MySQLdb()
将上述代码加入到settings.py中的即可,我这里直接放在DATABASES上:
# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
import pymysql
pymysql.install_as_MySQLdb()
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.mysql‘,
‘NAME‘: ‘hello_django_db‘,
‘USER‘:‘root‘,
‘PASSWORD‘:‘123456‘,
‘HOST‘:‘127.0.0.1‘,
‘PORT‘:‘3306‘,
}
}
接下来做数据库同步
Tools--->Run manage.py Task
makemigrations--->生成数据库脚本文件
migrate------>生成数据库表和数据
manage.py@hello_django > makemigrations
D:\JetBrains\bin\runnerw.exe C:\Users\Administrator\Documents\Workspace\venv\django_basic_venv\Scripts\python.exe D:\JetBrains\helpers\pycharm\django_manage.py makemigrations C:/Users/Administrator/Documents/Workspace/pycharm/hello_django
No changes detected
Following files were affected
Process finished with exit code 0
manage.py@hello_django > migrate
D:\JetBrains\bin\runnerw.exe C:\Users\Administrator\Documents\Workspace\venv\django_basic_venv\Scripts\python.exe D:\JetBrains\helpers\pycharm\django_manage.py migrate C:/Users/Administrator/Documents/Workspace/pycharm/hello_django
Operations to perform:
Apply all migrations: contenttypes, admin, sessions, auth
Running migrations:
Rendering model states... DONE
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 sessions.0001_initial... OK
Following files were affected
Process finished with exit code 0
django下数据库配置
标签:1.2 erro alter 配置 operation reg database default mic
本文系统来源:https://www.cnblogs.com/kindnull/p/8379769.html
内容总结
以上是互联网集市为您收集整理的django下数据库配置全部内容,希望文章能够帮你解决django下数据库配置所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。