码迷,mamicode.com
首页 > 数据库 > 详细

Django 数据库操作之数据库连接

时间:2019-01-24 00:28:32      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:options   ima   wmi   roo   format   mysqldb   nali   doc   from   

修改settings.py文件


"""
Django settings for db_operation_demo project.

Generated by ‘django-admin startproject‘ using Django 2.1.5.

For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = j@^55ibyauzd2c_*^09zp@+44pj5g8u4hq)lufepw1@$3u4z%)

# SECURITY WARNING: don‘t run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    django.contrib.admin,
    django.contrib.auth,
    django.contrib.contenttypes,
    django.contrib.sessions,
    django.contrib.messages,
    django.contrib.staticfiles,
]

MIDDLEWARE = [
    django.middleware.security.SecurityMiddleware,
    django.contrib.sessions.middleware.SessionMiddleware,
    django.middleware.common.CommonMiddleware,
    django.middleware.csrf.CsrfViewMiddleware,
    django.contrib.auth.middleware.AuthenticationMiddleware,
    django.contrib.messages.middleware.MessageMiddleware,
    django.middleware.clickjacking.XFrameOptionsMiddleware,
]

ROOT_URLCONF = db_operation_demo.urls

TEMPLATES = [
    {
        BACKEND: django.template.backends.django.DjangoTemplates,
        DIRS: [os.path.join(BASE_DIR, templates)]
        ,
        APP_DIRS: True,
        OPTIONS: {
            context_processors: [
                django.template.context_processors.debug,
                django.template.context_processors.request,
                django.contrib.auth.context_processors.auth,
                django.contrib.messages.context_processors.messages,
            ],
        },
    },
]

WSGI_APPLICATION = db_operation_demo.wsgi.application


# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
    default: {
        ENGINE: django.db.backends.mysql,
        NAME: django_db1,
        USER: root,
        PASSWORD: ‘passwd,
        HOST: localhost,
        PORT: 3306

    }
}


# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        NAME: django.contrib.auth.password_validation.UserAttributeSimilarityValidator,
    },
    {
        NAME: django.contrib.auth.password_validation.MinimumLengthValidator,
    },
    {
        NAME: django.contrib.auth.password_validation.CommonPasswordValidator,
    },
    {
        NAME: django.contrib.auth.password_validation.NumericPasswordValidator,
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = en-us

TIME_ZONE = Asia/Shanghai

USE_I18N = True

USE_L10N = True

USE_TZ = False


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = /static/

# pip3 install pymysql

修改__init__.py 文件

import pymysql
pymysql.install_as_MySQLdb()

 views.py

from django.shortcuts import render
from django.db import connection

def index(request):
    cursor = connection.cursor()
    # cursor.execute("insert into book(id,name,author) values(null,‘三国演义‘,‘罗贯中‘)")
    # cursor.executemany("insert into book(id,name,author) values(null,‘三国演义‘,‘罗贯中‘)")
    ‘‘‘执行多条sql语句‘‘‘
    cursor.execute("select * from book")
    # rows = cursor.fetchone()
    # rows = cursor.fetchmany(2)
    rows = cursor.fetchall()
    for i in rows:
        print(i)
    return render(request,index.html)

代码路径

 

技术分享图片

技术分享图片

Django 数据库操作之数据库连接

标签:options   ima   wmi   roo   format   mysqldb   nali   doc   from   

原文地址:https://www.cnblogs.com/randomlee/p/10312054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!