简体   繁体   English

python cursor.callproc() 函数不调用过程

[英]python cursor.callproc() function not calling procedure

New(ish) to SQL, just discovered stored procedures.新(ish)到 SQL,刚刚发现存储过程。

Restructuring a monolithic Python 3.7 script.重构一个整体的 Python 3.7 脚本。

Here's my stored procedure:这是我的存储过程:

CREATE PROCEDURE domSel(IN xip VARCHAR(16))
BEGIN
SELECT domain FROM pytest.nodes2 WHERE ip = xip
END;

It selects a domain based off an IP.它根据 IP 选择域。 The original code snippet in Python looks like this: Python 中的原始代码片段如下所示:

    for i,x,y in ip_ll:
        cursorA.execute("SELECT domain FROM pytest.nodes2 WHERE ip = '" + str(i) + "'")
        hosts_low.append(cursorA.fetchone())
        ips_low_latency.append(i)
        lat_low.append(x)

The following line is supposed to be the replacement:以下行应该是替换:

# The #s are of course the IP address.
result_arg = cursorA.callproc('domSel', args=('###.##.###.###,))
print(result_arg[1])

But it's giving me the error:但它给了我错误:

Traceback (most recent call last):
  File "./2queryMagic.py", line 45, in <module>
    print(result_arg[1])
IndexError: tuple index out of range

I tried to print arg[0], but of course that's the argument name.我试图打印 arg[0],但这当然是参数名称。 I tried iterating over the results in a for loop, this didn't work either... Python doesn't seem to be calling the procedure.我尝试在 for 循环中迭代结果,这也不起作用...... Python 似乎没有调用该过程。 Hopefully this is enough information, banging my head off the desk atm.希望这是足够的信息,把我的头从办公桌上敲下来。

As per rdas' comment, precedures require an out variable, and they must be referenced with @var_name根据 rdas 的评论,程序需要一个 out 变量,并且必须使用 @var_name 引用

call domSel('###.##.###.###', @dom);

(worked, but ofc relayed the actual domain)
+-------------------+
| domain            |
+-------------------+
| -domain.com      -|
+-------------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM