开发者

Using variables in Stored procedures with Python and MySQL

开发者 https://www.devze.com 2023-02-14 04:22 出处:网络
How would I setup a stored procedure in MySQL to use external variables. I have written SP before, but not with extenal inputs. With connection string coming from pyODBC.

How would I setup a stored procedure in MySQL to use external variables. I have written SP before, but not with extenal inputs. With connection string coming from pyODBC.

Then, using python how would I call that Sp and input that variable?

cursor.execute('Ca开发者_运维知识库ll d.MySP' ????)


Assuming you are using MySQLdb, according to the docs, the call would be:

cursor.callproc('d.MySP',args)

A peek under the hood shows how the variables are set and the CALL statement is made:

def callproc(self, procname, args=()):
    from types import UnicodeType
    db = self._get_db()
    charset = db.character_set_name()
    for index, arg in enumerate(args):
        q = "SET @_%s_%d=%s" % (procname, index,
                                     db.literal(arg))
        if isinstance(q, unicode):
            q = q.encode(charset)
        self._query(q)
        self.nextset()

    q = "CALL %s(%s)" % (procname,
                         ','.join(['@_%s_%d' % (procname, i)
                                   for i in range(len(args))]))
    if type(q) is UnicodeType:
        q = q.encode(charset)
    self._query(q)
    self._executed = q
    if not self._defer_warnings: self._warning_check()
    return args
0

精彩评论

暂无评论...
验证码 换一张
取 消