在第二个数据库上的Django调用存储过程

编程入门 行业动态 更新时间:2024-10-28 19:28:42
本文介绍了在第二个数据库上的Django调用存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在多数据库Django安装上调用存储过程,但没有运气获得结果.存储过程(位于辅助数据库上)在Django中始终返回一个空数组,但是在mysql客户端中执行时确实会出现预期的结果.

I'm trying to call a stored procedure on a multi-db Django installation, but am not having any luck getting results. The stored procedure (which is on the secondary database) always returns an empty array in Django, but the expected result does appear when executed in a mysql client.

我的 view.py 文件 从SomeDBModel导入模型 从django.db导入连接

My view.py file from SomeDBModel import models from django.db import connection

def index(request, someid): #Some related django-style query that works here loc = getLocationPath(someid, 1) print(loc) def getLocationPath(id, someval): cursor = connection.cursor() cursor.callproc("SomeDB.spGetLocationPath", [id, someval]) results = cursor.fetchall() cursor.close() return results

我也尝试过:

from SomeDBModel import models from django.db import connections def index(request, someid): #Some related Django-style query that works here loc = getLocationPath(someid, 1) print(loc) def getLocationPath(id, someval): cursor = connections["SomeDB"].cursor() cursor.callproc("spGetLocationPath", [id, someval]) results = cursor.fetchall() cursor.close() return results

每次打印出结果,我都会得到:

Each time I print out the results, I get:

[]

应检索的数据示例:

{ Path: '/some/path/', LocalPath: 'S:\Some\local\Path', Folder: 'SomeFolderName', Code: 'SomeCode' }

我还尝试过的一件事是打印 cursor.callproc 的结果.我得到:

One thing I also tried was to print the result of cursor.callproc. I get:

(id, someval)

此外,打印 cursor._exected 的结果将得出:

Also, printing the result of cursor._executed gives:

b'SELECT @_SomeDB.spGetLocationPath_arg1, @_SomeDB.spGetLocationPath_arg2'

似乎完全没有要运行的存储过程的引用.我什至尝试过将此作为最后的手段:

Which seems to not have any reference to the stored procedure I want to run at all. I have even tried this as a last resort:

cursor.execute("CALL spGetLocationPath("+str(id)+","+str(someval)+")")

但是我遇到关于需要 multi = True 的错误,但是将其放入execute()函数似乎并不像某些网站所建议的那样起作用,而且我不知道其他地方放在Django中.

but I get an error about needing multi=True, but putting it in the execute() function doesn't seem to work like some sites have suggested, and I don't know where else to put it in Django.

所以...我错过了什么主意?如何使存储过程正常工作?

So...any ideas what I missed? How can I get stored procedures to work?

推荐答案

我采取了以下步骤:

  • 将存储过程转储结果制作到临时表中,以便将结果集展平为单个结果集.这摆脱了multi=True
  • 的需要
  • 此外,我确保IP地址的用户可以访问数据库本身中的调用存储过程.
  • 最后,我继续研究 callproc 函数.最终,另一个站点上的某个人提出了以下有效的代码:

  • Made my stored procedure dump results into a temporary table so as to flatten the result set to a single result set. This got rid of the need for multi=True
  • In addition, I made sure the user at my IP address had access to call stored procedures in the database itself.
  • Finally, I continued to research the callproc function. Eventually someone on another site suggested the following code, which worked: cur = connections["SomeDB"].cursor() cur.callproc("spGetLocationPath", [id, someval]) res = next(cur.stored_results()).fetchall() cur.close()

  • 更多推荐

    在第二个数据库上的Django调用存储过程

    本文发布于:2023-10-22 20:05:27,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1518570.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:第二个   存储过程   数据库   Django

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!