简体   繁体   中英

python cursor.callproc() function not calling procedure

New(ish) to SQL, just discovered stored procedures.

Restructuring a monolithic Python 3.7 script.

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. The original code snippet in Python looks like this:

    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. I tried iterating over the results in a for loop, this didn't work either... Python doesn't seem to be calling the procedure. 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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