django使用mysql 好处_django使用mysql数据库

一、在mysql中手动创建空数据库

二、首页安装pymsql

方法1:file >> setting >> projext:项目名 >> Project Interpreter >>点击“+‘  >>搜索安装  pymysql

方法2: 打开控制端(CMD):使用pip  install pymysql 安装 (注点pip的版本,pip2 对应python2  pip3 对应python3),重启项目即可;

三、在django配置中 settings.py 中设置mysql连接

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

from django.db importmodels#Create your models here.

classswiper(models.Model):

img_name=models.CharField(max_length=225)

img_url=models.CharField(max_length=225)

img_target=models.CharField(max_length=225)

is_online=models.CharField(max_length=225)

weight=models.CharField(max_length=225)

belong_to=models.CharField(max_length=225)

View Code

四、在settings.py 统计文件 __init__py 中导入使用pymysql

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

from pymysql importinstall_as_MySQLdb

install_as_MySQLdb()

View Code

如果出现报错: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

解决办法:__init__.py文件中

importpymysql

pymysql.version_info= (1, 4, 6, 'final', 0) #change mysqlclient version3

pymysql.install_as_MySQLdb()

72a261945b3c09c6470a8c7c7b8cd658.png

五、在models.py 中创建表

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

from django.db importmodels#Create your models here.

classswiper(models.Model):

img_name=models.CharField(max_length=225)

img_url=models.CharField(max_length=225)

img_target=models.CharField(max_length=225)

is_online=models.CharField(max_length=225)

weight=models.CharField(max_length=225)

belong_to=models.CharField(max_length=225)

View Code

六、编译创建表

>> python manage.py makemigrations

492f674a7e56edac424c336648725497.png

>>  python manage.py migrate

2010c54c28c817562f97214873eabfd2.png

7、在代码中导入models模块,进行数据库操作

导入models: from app名称  import models

577c2dfed41df8ecca6c4ee5a57e89e5.png

之后就可以正常操作了

  • 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、付费专栏及课程。

余额充值