1. 程式人生 > >django下數據庫配置

django下數據庫配置

ati cte part del 錯誤 proc tool sqli sqlit

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下數據庫配置