Django-mysql Database Usage 01

Source: Internet
Author: User

Django Connection Database requirements: 1) pycharm run the Django platform; 2) MySQL database. The premise of this article is that all two platform readers have been installed correctly, not installed friends please Baidu. The Django I used is version 2.1, which differs from the old version when connecting data. The Pycharm feature is very powerful and all MySQL command operations can be implemented in Pycharm.

First, Pycharm connection database

Find the MySQL data source

Set the user name and password to connect to the database, typically the root user

Once set up, you can view the contained database by command-line operation

Database display

In the future, all related operations on the data can be operated in Pycharm, which is very convenient.

Second, the database connection configuration

python2.x series Connection MySQL database needs MySQLdb class library support, python3.x series connection MySQL need Pymysql class library support,

python3.6 Mounting Module Pymysql:

1. Command line install pip install Pymysql,

2. Use the Pycharm supported Class library installation, the installation process and the Django installation process in two ways

3. Also need to install PIP install Mysqlclient, so that the import MySQLdb will not error

See if the installation package is installed in Pycharm

OK, everything is well, start coding.

Create a database

Making a database connection in settings.py

Write the database logic in models.py, where the class name corresponds to the table in the database, giving only one property as a demonstration

Next, build the table structure and enter in the terminal:

View on the database side

To view the tables in the database Deng

From the results, we can see that there are many tables, but our own is only the Sayhello_employee table established at that time. Let's look at the information in this table again:

In the table, when the Django Orm creates a table, an ID field (default, primary key, autogrow), a Name field, is defined in the models.py, and the maximum length is 30, which is reflected

The first section of the database operation is complete

Django-mysql database uses a

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.