Django中更改默认数据库为mysql的方法示例

Django中默认使用sqlite3数据库,今天研究了下如何将它换成常见的mysql数据库。

由于项目用得python3,而MySQLdb没有支持python3的版本,如果使用python3.x版本时,pip install MySQLdb时会报错。

后来通过谷歌发现可以使用pymysql替代MySQLdb

1 在项目根目录下的__init__.py文件中加入如下代码:

import pymysql
pymysql.install_as_MySQLdb()

2 使用mysqlclient代替MySQLdb,安装方式为:

pip install mysqlclient

3 更改项目setting.py中对数据库的配置为:

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'test',
    'USER': 'username',
    'PASSWORD': 'passwd',
    'HOST': 'localhost',
    'PORT': '3306'
  }
}

4 最后通过python manage.py migrate命令,Django会在数据库中自动创建相应的表

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running 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 admin.0003_logentry_add_action_flag_choices... 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 auth.0009_alter_user_last_name_max_length... OK
  Applying polls.0001_initial... OK
  Applying sessions.0001_initial... OK

5 在创建admin用户时,遇到了如下报错

python manage.py createsuperuser
Superuser creation skipped due to not running in a TTY. You can run `manage.py createsuperuser` in your project to create one manually.

后来查了一下,是因为使用了git来执行命令,切换到Windows自带的命令行,可以解决该问题!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • django+mysql的使用示例

    Django中每一个模型model都对应于数据库中的一张表,每个模型中的字段都对应于数据库表的列.方便的是,django可以自动生成这些create table, alter table, drop table的操作.其次Django为咱们也提供了后台管理模块(Django-Admin),主要功能是通过后台管理来实现客户端的功能,可以对数据进行增删改查.也可以通过该功能进行二次开发 Django中的mysql配置 在上期内容,我们了解了settings.py配置信息的内容,其中DATABASES

  • Django+mysql配置与简单操作数据库实例代码

     第一步:下载mysql驱动 cmd进入创建好的django项目目录:使用命令 pip install mysqlclient 等待安装成功! 第二步:在settings.py中配置mysql连接参数(没有mysql的先装mysql) DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': '数据库名(你得先在mysql中创建数据库)', 'USER':'mysql用户名(如root)', 'PASSWOR

  • Django使用Mysql数据库已经存在的数据表方法

    使用scrapy爬取了网上的一些数据,存储在了mysql数据库中,想使用Django将数据展示出来,在网上看到都是使用Django的models和makemigration,migrate命令来创建新表,并使用. 可是我的数据已经存在了已经创建好,并且已经存储有数据了,不能再重新创建新表了. 了解Django的表明和models名称的映射关系就可以让Django使用已经存在的表. 假如在Django存在models如下: from django.db import models # Create

  • python Django连接MySQL数据库做增删改查

    1.下载安装MySQLdb类库http://www.djangoproject.com/r/python-mysql/2.修改settings.py 配置数据属性 复制代码 代码如下: DATABASES = {    'default': {        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.        'NAME': 'djang

  • Django如何配置mysql数据库

    Django项目默认使用sqlite 数据库,但是我想用mysql数据库,应该如何配置呢. Django连接mysql数据库的操作,是通过根模块的配置实现的,在项目根模块的配置文件settings.py中,我们可以查询到如下DATABASES的配置信息: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } 解释一下上

  • python django 增删改查操作 数据库Mysql

    下面介绍一下django增删改查操作: 1.view.py # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.http import HttpResponse from polls.models import Test from django.shortcuts import render # Create your views here. # 解决乱码 import sys reload(sy

  • 使用Django连接Mysql数据库步骤

    链接mysql步骤 第一步:在终端下载pymysql文件–pip install pymysql 第二步:在gjango项目的__init__文件中添加代码 import pymysql pymysql .install_as_MySQLdb() 第三步:找到mysql的连接源,然后填信息,如果没有mysql驱动,需要下载MySQL驱动 mysql的配置:进入settings文件中找到DATABASES配置信息 DATABASES = { 'default': { 'ENGINE': 'djan

  • Django数据库类库MySQLdb使用详解

    Django项目要操作数据库,首先要和数据库建立连接,才能让程序中的数据和数据库关联起来进行数据的增删改查操作 Django项目默认使用mysqldb模块进行和mysql数据库之间的交互操作. 下面看下Django进行数据库操作的步骤: 1. 修改settings.py 配置数据项 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mydatabase', 'USER': 'mydatabaseus

  • Python django使用多进程连接mysql错误的解决方法

    问题 mysql 查询出现错误 error: (2014, "Commands out of sync; you can't run this command now")1 查询 mysql文档中的解释 If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.   Thi

  • Django安装配置mysql的方法步骤

    近期做那个python的开发,今天就来简单的写一下Django安装配置mysql的方法步骤 安装mysql 首先安装pymysql pip install pymysql 然后安装mysqlclient pip install mysqlclient 在安装过程中可能会报类似以下错误(因为我的已经安装成功了,找了类似的图): 在window下安装python的包会出现各种问题,https://www.lfd.uci.edu/~gohlke/pythonlibs/这个网站就是专门用于解决windo

  • Django框架使用mysql视图操作示例

    本文实例讲述了Django框架使用mysql视图操作.分享给大家供大家参考,具体如下: 一.Mysql视图的创建 MySQL中,在两个或者以上的基本表上创建视图,例如:在StudentOrm表和InfoOrm表上,创建mysql_view_test_orm视图 1.首先,创建两张表 from django.db import models # Create your models here. class StudentOrm(models.Model): name = models.CharFi

随机推荐