django mysql connector_MySQL Connector / Python作为Django引擎?

即使经过数小时和数小时的谷歌搜索,也无法找到答案.搜索堆栈溢出.我向你们保证,我已经看到了所有可能被视为相关的答案,但这些答案都没有解决我所面临的问题.无需再费周折 –

目前在shell中我可以这样做:

Python 2.7.11+ (default, Apr 17 2016, 14:00:29)

[GCC 5.3.1 20160413] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> from distutils.sysconfig import get_python_lib

>>> print get_python_lib()

/usr/lib/python2.7/dist-packages

>>> import mysql.connector

>>> db = mysql.connector.connect(user='root', password='test123', host='127.0.0.1', database='mydb')

>>> db

验证我已安装此模块.但是,当我尝试转到settings.py文件以设置DATABASE ENGINE时

DATABASES = {

'default': {

'ENGINE': 'mysql.connector.django',

'NAME': 'mydb',

'USER': 'root',

'PASSWORD': 'test123',

'HOST': '127.0.0.1',

'PORT': '3306'

}

}

并尝试启动我的服务器我一直收到错误:

django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django'

isn't an available database backend.

不知道如何解决这个问题.我在Django版本1.9.7上,Python版本显示在上面的代码片段中

使用’mysql.connector.django’与使用’django.db.backends.mysql’之间有什么区别?

最佳答案

What would be the difference between using ‘mysql.connector.django’

vs. using ‘django.db.backends.mysql’ ?

从前者开始是推荐的方法,而后者则不是.前者完全支持,但后者需要1.1.x版本,django文档中还有一个警告,即更新版本的django可能无法完全支持它.

The Python Database API is described in PEP 249. MySQL has three

prominent drivers that implement this API:

MySQLdb is a native driver that has been developed and supported for

over a decade by Andy Dustman. mysqlclient is a fork of MySQLdb which

notably supports Python 3 and can be used as a drop-in replacement for

MySQLdb. At the time of this writing, this is the recommended choice

for using MySQL with Django. MySQL Connector/Python is a pure Python

driver from Oracle that does not require the MySQL client library or

any Python modules outside the standard library. All these drivers are

thread-safe and provide connection pooling. MySQLdb is the only one

not supporting Python 3 currently.

In addition to a DB API driver, Django needs an adapter to access the

database drivers from its ORM. Django provides an adapter for

MySQLdb/mysqlclient while MySQL Connector/Python includes its own.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Django 架构中,一般使用 Django 自带的 ORM(Object-Relational Mapping)来操作数据库。ORM 是一种将关系数据库中的数据映射到对象的技术,将程序中的对象转换为数据库中的记录,从而实现对数据库的操作。 不过,如果你想直接使用 MySQL Connector/Python 驱动来连接 MySQL 数据库,可以在 Django 项目中的 `__init__.py` 文件中进行相关配置。具体配置如下: 1. 安装 MySQL Connector/Python 驱动,可以使用 pip 命令进行安装: ``` pip install mysql-connector-python ``` 2. 在 Django 项目的 `__init__.py` 文件中添加以下代码: ```python import mysql.connector from django.db.backends.mysql.base import DatabaseWrapper # 定义 MySQL Connector/Python 驱动的数据库连接参数 cnx = mysql.connector.connect(user='your_username', password='your_password', host='your_host', database='your_database') # 重写 DjangoMySQL 数据库连接方法 class MySQLDatabaseWrapper(DatabaseWrapper): def get_new_connection(self, conn_params): return cnx # 注册 MySQLDatabaseWrapper DatabaseWrapper.register_backend('mysql.connector', 'django.db.backends.mysql.base', 'MySQLDatabaseWrapper') ``` 需要将上述代码中的 `your_username`、`your_password`、`your_host` 和 `your_database` 替换为实际的数据库连接参数。 3. 在 Django 项目的 `settings.py` 文件中,将 `DATABASES` 配置中的 `ENGINE` 改为 `django.db.backends.mysql.connector`,如下所示: ```python DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql.connector', 'NAME': 'your_database', 'USER': 'your_username', 'PASSWORD': 'your_password', 'HOST': 'your_host', 'PORT': 'your_port', } } ``` 需要将上述代码中的 `your_username`、`your_password`、`your_host`、`your_database` 和 `your_port` 替换为实际的数据库连接参数。 这样,就可以在 Django 项目中使用 MySQL Connector/Python 驱动来连接 MySQL 数据库了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值