Article 3 How to Use ORM to establish a connection with data for ORM object relationship ing

Source: Internet
Author: User

 

1. ORM (Object relationship ing) this concept is applied in many web frameworks in many languages.

 



2. Why is there an Orm?
1. Writing programs is inseparable from data.
2. What should I do if I need to use the data in the database in a python program?
1. Use pymysql to connect to the MySQL database
1. Import pymysql
2. Establish a connection
Conn = pymysql. Connect (
Host = '2017. 0.0.1 ',
Port = 3306,
Database = 'day43 ',
User = 'root ',
Password = '000000 ',
Charset = 'utf8'
)
3. Get the cursor object
Cursor = conn. cursor () --> [(), ()] is returned by default.
Cursor = conn. cursor (cursor = pymysql. cursors. dictcursor) --> [{},{}] is returned by default.
4. Execute SQL statements
Cursor.exe cute ('select * From userinfo ')
Cursor.exe cute ('select * From userinfo where username = % s and Password = % s', ['xyh', 'xyh123 '])
5. Get Results
1. cursor. fetchone ()
2. cursor. fetchall ()
3. cursor. fetchmany (N)
3. What is object relational ing?
1. New syntax. We do not need to write SQL statements by ourselves.
2. We write code according to the new syntax. He helps me translate it into SQL statements.
4. Advantages and Disadvantages of ORM
Advantages of ORM:
1. High development efficiency
2. Easy to master
3. Easy to transplant
Disadvantages of ORM:
1. Low code execution efficiency
5. Orm focus:
Class <--> data table
Attribute <--> Field
Object <--> data row
6. What can ORM do?
1. Operate Data Tables
2. Operate data rows
7. How to Use ORM in Django?
1. Tell the Django framework which database to connect
Databases = {
'Default ':{
'Engine': 'django. DB. backends. mysql', # Tell Django to connect to the MySQL database
'Name': 'day57', # Database Name
'Host': '127. 0.0.1 ', # Database IP Address
'Port': 3306, # Database Port
'User': 'root', # account used to connect to the data
'Password': '000000' # password used to connect to the database
}
}

 

 


2. Tell the Django framework what to connect to the database
In the _ init _. py file in the same directory as settings. py:
Import pymysql
Pymysql. install_as_mysqldb ()


3. Define classes in the models. py file under the app
Class class name (models. Model ):
Pass

 


4. Two commands
1. Python manage. py makemigrations --> record models. py changes on the small book (APP/migrations)
2. Python manage. py migrate --> translate change records into SQL statements and execute them in the database.
8. Library Management System
1. Table Structure
1. Press
2. Books
3. Author table
4. Relationship between authors and books

2. add, delete, modify, and query of the publishing house
1. Query
1. Orm: publisher. Objects. All () --> query data of all publishers
2. Write a For Loop in HTML (template LANGUAGE)
{% For I in [...,...] %}
{% Endfor %}

 


2. Add
1. Create data with ORM
Publisher. Objects. Create (name = 'new publisher name ')
3. Delete
1. Add parameters to URLs in HTTP
/Delete_publisher /? Id = 1
2. How to obtain URL parameters in Django
Request. Get --> A large dictionary
Request. Get. Get ('id') --> Value
3. delete operations in ORM
Publisher. Objects. Filter (ID = 1). Delete ()
4. Change
1. Orm Modification
OBJ. Name = 'new publisher name'
OBJ. Save () --> submit the changes to the database


Article 3 How to Use ORM to establish a connection with data for ORM object relationship ing

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.