Django Connection data MySQL

Source: Internet
Author: User
Tags mysql host

Background

System centos6.x

Python version 3.4.3

Django 1.9.x


The default Django database is Sqlite3, and we often use MySQL



We need to connect MySQL at this time is the need to Django connection MySQL driver, the default is MySQLdb, but this is difficult to download installation, in the CentOS environment is PIP is unable to install



So you need to replace it with Pymysql (install from package pymysql)

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/80/71/wKiom1dBosLzOwlhAAAkOPLQQcw922.png "title=" Pip1.png "alt=" Wkiom1dboslzowlhaaakoplqqcw922.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/80/6F/wKioL1dBpCXTRebbAABQpxO_caY395.png "title=" Pip2.png "alt=" Wkiol1dbpcxtrebbaabqpxo_cay395.png "/>



"To begin the steps to replace the default MySQLdb tool"

1 Add the Use Pymsql as MySQLdb under the app directory under the project

[email protected] firstproject]# cat blog/__init__.py

#!/usr/bin/env Pythonimport pymysqlpymysql.install_as_mysqldb ()



2 Modify manage.py join using Pymysql


[email protected] 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)



"Connection to database Configuration"

You have to use the data, you have to indicate 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.sql Ite3 '),}}


Modified to:

DATABASES = {' Default ': ' ENGINE ': ' Django.db.backends.mysql ', ' NAME ': ' blog ', ' USER ': ' Root ', ' PASSWORD ': ' 12qwaszx ', ' HOST ': ' localhost ', ' PORT ': ',}}



Contains, MySQL host ports, users and libraries (blogs)


Create a blog in your MySQL library

MYSQL-UROOT-P12QWASZX-E ' CREATE database blog character Set UTF8 '
Mysql-uroot-p12qwaszx-e "Show databases like '%blog% '"
+-------------------+| Database (%blog%) | +-------------------+| Blog |+-------------------+[[email protected] ~]#



"Django builds the Database model (table) and operational database"


[Email protected] 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 model that defines the people class inherits the database, defines the class model (table) with a field Name field, and customizes the table named self_people!

Class meta:db_table = ' Self_people '

The purpose of this is to customize the table name, not the way that Django generates the table name!




"Synchronizing table Information"

Python manage.py Makemigrationspython manage.py Migrate


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/80/71/wKiom1dBqzeDZhQ6AACdZHgpHXE095.png "title=" Django1.png "alt=" Wkiom1dbqzedzhq6aacdzhgphxe095.png "/>


View information about viewing tables from a database: (I'm using the Navicat GUI to manage MySQL tools to view)

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/80/6F/wKioL1dBrKrzTUg6AADKds3lAdI739.png "title=" Django1.png "alt=" Wkiol1dbrkrztug6aadkds3ladi739.png "/>





"Operational Database"--we use the interactive form provided by Django to invoke the API interface provided by Djangol


Python manage.py Shell


First: Import the People class

From Blog.models import people

Second: View the entries that begin in this table

People.objects.count ()


Third: Create an entry:

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

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/80/71/wKiom1dBr4_BAumyAAAoF9uPuSU580.png "title=" Shell1.png "alt=" Wkiom1dbr4_baumyaaaof9upusu580.png "/>



IV: View all the data in the table

in [+]: p = People.objects.all () in [+]: p.values_list () out[17]: [(1, ' Liudehua '), (2, ' zhangxueyou ')]








This article is from the "Tridewah operation and maintenance work Road" blog, please be sure to keep this source http://cuidehua.blog.51cto.com/5449828/1775972

Django Connection data MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.