django 链接多个数据库 并使用原生sql实现
作者:喝完这杯还有一箱 时间:2024-01-23 12:52:06
settings文件如下:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'db1': { # 配置第二个数据库节点名称
'ENGINE': 'django.db.backends.oracle',
'NAME': 'devdb',
'USER': 'hysh',
'PASSWORD': 'hysh',
'HOST': '192.168.191.3',
'PORT': '1521',
},
}
查找Django的文档:
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
上述方法是设置中如果有多个数据库,会默认使用 default,当你想使用指定的数据库连接时,引入的对象就变成了connections !
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
之后再进行操作。
补充知识:Django多数据源接入类
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework import status
from django.db import transaction
from .contants import db_dict
contants.py的内容
(
import cx_Oracle
import pymysql
# 定义一个数据库类型&引擎的字典,
db_dict = {'mysql':pymysql,'Oracle':cx_Oracle}
)
from .models import DataSystem,Rule
class DBconnectView(GenericAPIView):
__DBtype = db_dict
def get(self,request,pk,rule_id):
# 通过传入的id进行对应的数据库链接
self.datas = DataSystem.objects.get(pk=pk)
self.url = self.datas.url
self.username = self.datas.username
self.password = self.datas.password_enc
self.DBname = self.datas.name
self.DBtype = self.__DBtype[self.datas.type]
# 获取check_code规则
self.ruledatas = Rule.objects.get(id=rule_id)
self.check_code = self.ruledatas.check_code
# db = __import__(self.DBtype)
try:
conn = self.DBtype.connect(host=self.url,user=self.username,password=self.password,database=self.DBname)
# 链接成功后创建一个游标
cs_ms = conn.cursor()
except Exception as e:
raise e
else:
# 明显的开启事务
with transaction.atomic():
# 在安全的地方,创建保存点,将来操作数据库失败回滚到此
save_id = transaction.savepoint()
try:
# 获取一个元组
db_ret = cs_ms.execute(self.check_code)
except Exception as e:
transaction.savepoint_rollback(save_id)
raise e
else:
db_set = db_ret.fetchone()
# transaction.savepoint_commit(save_id)
finally:
cs_ms.close()
conn.close()
return Response({'pk':pk,'rule_id':rule_id})
来源:https://blog.csdn.net/qq_37049050/article/details/85131514
标签:django,数据库,原生sql
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python在画图时使用特殊符号的方法总结
2021-12-27 22:29:34
![](https://img.aspxhome.com/file/2023/2/115102_0s.jpg)
PHP中文字符串截断无乱码解决方法
2024-05-11 09:44:55
给你选择Python语言实现机器学习算法的三大理由
2023-08-20 23:10:26
![](https://img.aspxhome.com/file/2023/0/123690_0s.png)
Python 平方列表中每个数字的多种操作
2023-11-14 03:53:00
![](https://img.aspxhome.com/file/2023/8/64618_0s.jpg)
QML使用Python的函数过程解析
2021-12-10 19:20:31
使用Tensorboard工具查看Loss损失率
2022-04-17 12:50:30
![](https://img.aspxhome.com/file/2023/0/105780_0s.jpg)
Python unittest discover批量执行代码实例
2023-08-14 14:05:37
Python中的异常处理讲解
2023-01-29 11:22:57
![](https://img.aspxhome.com/file/2023/5/107825_0s.jpg)
Python的Twisted框架中使用Deferred对象来管理回调函数
2021-01-04 09:01:23
![](https://img.aspxhome.com/file/2023/6/133716_0s.png)
python爬虫系列Selenium定向爬取虎扑篮球图片详解
2021-02-19 17:21:52
![](https://img.aspxhome.com/file/2023/4/133244_0s.jpg)
MySQL简单了解“order by”是怎么工作的
2024-01-16 10:52:41
![](https://img.aspxhome.com/file/2023/5/127565_0s.png)
SQLSERVER的非聚集索引结构深度理解
2024-01-20 01:54:00
![](https://img.aspxhome.com/file/2023/1/85601_0s.bmp)
python算法与数据结构之单链表的实现代码
2022-09-30 14:35:39
![](https://img.aspxhome.com/file/2023/0/98820_0s.png)
围观tangram js库
2024-04-29 13:38:39
Python标准库calendar的使用方法
2023-09-01 01:28:07
Python类中的魔法方法之 __slots__原理解析
2023-10-01 18:49:51
SQL Server 自动增长清零的方法
2012-01-05 19:07:47
解析优化MySQL插入方法的五个妙招
2024-01-19 13:13:49
GO语言文件的创建与打开实例分析
2024-05-21 10:21:19
JS实现新建文件夹功能
2024-04-16 09:50:15
![](https://img.aspxhome.com/file/2023/2/136542_0s.gif)