当前位置:网站首页>Django note 21: querying databases using native SQL
Django note 21: querying databases using native SQL
2022-06-12 13:21:00 【VV Ann's shallow singing】
Django Provides two ways to execute native SQL Code .
One is to use raw() function , One is Use connection.cursor().
But the authorities still recommend using native SQL Before , Try to explore first QuerySet A variety of API.
For now , Official documents provide a variety of API It can meet most application scenarios .
The following is the table of contents of this note :
- raw()
- connection.cursor()
- Multi database operation
1、raw()
This method can be used to manipulate native SQL, Then return model example :
We use Blog As an example , The code used is as follows :
for blog in Blog.objects.raw("select * from blog_blog"):
print(blog)
Copy code The effect of the above code is similar to Blog.objects.all() The results obtained are the same , But some operations are different , such as all().count() Can be obtained total , however raw() You can't do this .
It should be noted that ,raw() It doesn't detect the input SQL Code , Even if we use Blog This model To query Entry The data of , You can also return results , But all they return are Entry Table properties :
for blog in Blog.objects.raw("select * from blog_entry"):
print(blog.__dict__) # __dict__ The output is blog_entry Field of
Copy code in other words stay Blog.objects.raw() in , What really works is raw() This function , Ahead Blog It's just a shelf , Or way , It's to elicit raw() This function .
Custom return fields
stay raw() Functional SQL In the code , We can customize the selected fields , If you need to use a field that is not selected , Then the system will access the database again to get , The operation process is the same as that described above defer() The function is the same .
for item in Blog.objects.raw("select id from blog_blog"):
print(item.__dict__)
print(item.id, item.name)
print(item.__dict__)
{'_state': <django.db.models.base.ModelState object at 0x7fbd4165b6a0>, 'id': 2}
2 hunter
{'_state': <django.db.models.base.ModelState object at 0x7fbd4165b6a0>, 'id': 2, 'name': 'hunter'}
Copy code You can see , Among the returned results, the first output data is only id, Back , When we visit name Field time , I went to get it again name Field data .
Custom fields must contain primary keys
When we return a custom field , Must be to contain the primary key field , Otherwise, we will report an error when we get the information , For example, the following operation :
for blog in Blog.objects.raw("select name from blog_blog"):
print(blog.__dict__)
Copy code stay print(blog.dict) It's a mistake , There is no primary key information in the data
Custom return new field
Can follow QuerySet Of annotate Same operation , Custom new fields return , When getting, you can return directly according to the attribute value , such as :
entry = Entry.objects.raw("select *, date_format(pub_date, '%%Y-%%m') as date_1 from blog_entry")[0]
print(entry.date_1)
Copy code Passing variables
For input SQL Statement to pass variables :
name = "python"
Blog.objects.raw("select * from blog_blog where name = '%s'", [name])
Copy code 2、connection.cursor()
Django Introduced a more direct implementation SQL The way , The modules used are django.db.connction, Use of cursor and pymysql The library is used in the same way , The official example is as follows :
from django.db import connection
def my_custom_sql(self):
with connection.cursor() as cursor:
cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz])
cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
row = cursor.fetchone()
return row
Copy code We need to pay attention to , If a parameter is passed in SQL in , You need to escape some symbols before you can use , such as :
cursor.execute("SELECT foo FROM bar WHERE baz = '30%'")
cursor.execute("SELECT foo FROM bar WHERE baz = '30%%' AND id = %s", [self.id])
Copy code among , Of the second sentence % Need to escape
adapter fetch The data of
adopt fetchone and fetchall() The data returned is only value It's worth it , There is no corresponding field key, If you can sacrifice performance and memory properly , In exchange for the convenience and accuracy of obtaining data , Officials offer such a way :
def dictfetchall(cursor):
"Return all rows from a cursor as a dict"
columns = [col[0] for col in cursor.description]
return [
dict(zip(columns, row))
for row in cursor.fetchall()
]
Copy code After we finish cursor.execute(sql) after , hold cursor Pass in as a parameter dictfetchall Function , You can return a list of dictionaries
Introduce to you cursor.description, This returns a tuple of data , The element inside is also a tuple , The first element of a tuple is us select Field name .
therefore columns = [col[0] for col in cursor.description] This line of code gets all the specified field names
Examples of use :
def dictfetchall(cursor):
"Return all rows from a cursor as a dict"
columns = [col[0] for col in cursor.description]
return [
dict(zip(columns, row))
for row in cursor.fetchall()
]
with connection.cursor() as cursor:
sql = "select id, name from blog_blog"
cursor.execute(sql)
results = dictfetchall(cursor)
print(results)
# The result returned :
# [{'id': 20, 'name': 'name_3'}, {'id': 21, 'name': 'name_4'}, {'id': 1, 'name': 'name_5'}]
Copy code In the process of my use , We use the context manager to get the cursor :
with connection.cursor() as cursor:
cursor.execute()
Copy code therefore , After use , There is no need to manually close , He is consistent with the following usage effect :
c = connection.cursor()
try:
c.execute(...)
finally:
c.close()
Copy code But the recommended way is the context manager , More elegant .
3、 Multi database operation
If the system uses multiple databases , So in use cursor When , You need to use it django.db.connections modular :
from django.db import connections
with connections['my_db_alias'].cursor() as cursor:
pass
Copy code The above is the whole content of this note , The next note will briefly introduce the operation of multiple databases .
This article starts with my official account of WeChat. :【Django note 】.
边栏推荐
- [wechat applet development] Part 1: development tool installation and program configuration
- 章鱼网络进展月报 | 2022.5.1-5.31
- Known as the next generation monitoring system! See how awesome it is
- Redis消息队列重复消费问题
- 基于华为云鲲鹏弹性云服务器ECS部署openGauss数据库【这次高斯不是数学家】
- Software construction 03 regular expression
- 大一女生废话编程爆火!懂不懂编程的看完都拴Q了
- 镜像扫描工具预研
- 【刷题篇】抽牌获胜的概率
- [you code, I fix] whitesource was officially renamed mend
猜你喜欢

Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver

入门深度学习与机器学习的经验和学习路径

Newoj week 10 question solution

Eight misunderstandings are broken one by one (2): poor performance? Fewer applications? You worry a lot about the cloud!

简历 NFT 平台 TrustRecruit 加入章鱼网络成为候选应用链

Build an embedded system software development environment - build a cross compilation environment

Actual combat | realizing monocular camera ranging by skillfully using pose solution

unittest框架

403 you don't have permission to access this resource

Summary of question brushing in leetcode sliding window
随机推荐
Microsoft Word 教程,如何在 Word 中插入页眉或页脚?
【云原生 | Kubernetes篇】深入了解Ingress
Innovation training (XII) project summary
MUI登录数据库完善与AJAX异步处理【MUI+Flask+MongoDB+HBuilderX】
【刷题篇】超级洗衣机
Source of routing information
unittest框架
The goods are full. You must take this knowledge
[you code, I fix] whitesource was officially renamed mend
list和dict的应用
Embedded system hardware composition - embedded system hardware architecture
Hardware composition of embedded system - introduction of embedded development board based on ARM
A brief introduction to Verilog mode
Design virtual network to realize communication between virtual machine instance and external network
There was an error installing mysql. Follow the link below to CMD
442 authors, 100 pages! It took Google 2 years to release the new benchmark big bench | open source
import torch_geometric 加载一些常见数据集
VGA显示彩条和图片(FPGA)
Software construction 03 regular expression
Structure matérielle du système embarqué - introduction du Conseil de développement embarqué basé sur arm