Get the App
SLTechnology News&Howtos  ›  Database  › 

Django connection data mysql

Shulou Source: shulou.com Published: 2022-06-01 17:44:56 10月07日 Update

[background]

System centos6.x

Python version 3.4.3

Django 1.9.x

The database used by the default django is sqlite3, while the one we often use is mysql

We need to connect to mysql. At this time, we need django to connect to the driver of mysql. The default is MySQLdb, but this is very difficult to download and install. Pip cannot be installed in centos environment.

So you need to use pymysql instead (install from package PyMySQL)

[start the steps to replace the default MySQLdb tool]

1 add pymsql as MySQLdb in the app directory under the project

[root@master firstproject] # cat blog/__init__.py

#! / usr/bin/env pythonimport pymysqlpymysql.install_as_MySQLdb ()

2 modify manage.py to join using pymysql

[root@master firstproject] # cat manage.py

#! / usr/bin/env python

Import os

Import sys

Try: import pymysql pymysql.install_as_MySQLdb () except Exception: pass

If _ name__ = = "_ _ main__":

Os.environ.setdefault ("DJANGO_SETTINGS_MODULE", "firstproject.settings")

From django.core.management import execute_from_command_line

Execute_from_command_line (sys.argv)

[configuration of connecting to database]

If you want to use data, you must specify some information about the data in the configuration file.

Rm-f db.sqlite3 this can be deleted, we do not use sqlite text database

DATABASES = {'default': {' ENGINE': 'django.db.backends.sqlite3',' NAME': os.path.join (BASE_DIR, 'db.sqlite3'),}}

Modified to:

DATABASES = {'default': {' ENGINE': 'django.db.backends.mysql',' NAME': 'blog',' USER': 'root',' PASSWORD': '12qwaszx,' HOST': 'localhost',' PORT':'',}

Contains, mysql's host port, user and library (blog)

Create the blog library in your mysql

Mysql-uroot-p12qwaszx-e'create database blog character set utf8' mysql-uroot-p12qwaszx-e "show databases like'% blog%'" +-+ | Database (% blog%) | +-+ | blog | +-+ [root@master ~] #

[django builds database model (table) and manipulates database]

[root@master firstproject] # vim blog/models.py

From django.db import models# Create your models here.class People (models.Model): name = models.CharField (max_length=32) class Meta: db_table = 'self_people'

Note: the People class inherits the database model, defines that the class model (table) has a field name field, and customizes the table name as self_people!

Class Meta: db_table = 'self_people'

The purpose of writing this is to customize the table name, not to have django generate the table name in the same way!

[synchronization table information]

Python manage.py makemigrationspython manage.py migrate

View information about viewing tables from the database: (I used the navicat GUI management mysql tool to view)

[manipulate database]-- here we use the interactive form provided by django to call the api interface provided by djangol

Python manage.py shell

First: import the People class

From blog.models import People

Second: look at the starting entries in this table

People.objects.count ()

Third: create an entry:

People.objects.create (name= "liudehua") People.objects.create (name= "zhangxueyou")

Fourth: view all the data in the table

In [16]: P = People.objects.all () In [17]: p.values_list () Out [17]: [(1, 'liudehua'), (2,' zhangxueyou')]

Tags: Data database information model fields tools entries configuration host interactive common form interface file text method step version environment user Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Huawei Docker Shulou Tech Info OPPO Reno