python - 如何使用sqlalchemy调用存储过程

标签 python mysql google-app-engine sqlalchemy

如何使用sqlAlchemy调用MySQL的存储过程?

我尝试了以下代码:

import webapp2

from sqlalchemy import *
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import create_session
from sqlalchemy import create_engine

class TestSQL(webapp2.RequestHandler):

def get(self):
    Base = automap_base()

    engine = create_engine(('mysql://root@127.0.0.1:3306/testDB?unix_socket=/cloudsql/School=:mysqlserver'))
    # reflect the tables
    Base.prepare(engine, reflect=True)

    metadata = MetaData(bind=engine)
    users = Table('Student', metadata, autoload=True)
    student = metadata.tables ["Student"]

    session = create_session()

    pro = session.execute("p_get_teacher_requests", (1,0))
    self.response.write(pro)

这是我得到的错误:

UnboundExecutionError: Could not locate a bind configured on SQL expression or this Session

最佳答案

经过一些研究和多次试验后,它可以使用以下代码:

import webapp2

from sqlalchemy import *
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import create_session
from sqlalchemy import create_engine


class TestSQL(webapp2.RequestHandler):
    def get(self):
        self.response.headers["Content-Type"] = "application/json"

        Base = automap_base()

        engine = create_engine(('mysql://root@IP:3306/School?unix_socket=/cloudsql/school:mysqlserver'))

        # reflect the tables
        Base.prepare(engine, reflect=True)

        metadata = MetaData(bind=engine)

        users = Table('Student', metadata, autoload=True)
        student = metadata.tables ["Student"]

        session = create_session()

        connection = engine.raw_connection()
        cursor = connection.cursor()
        cursor.callproc("p_get_teacher_requests", [1,0])
        results = list(cursor.fetchall())
        cursor.close()
        connection.commit()

        self.response.write(results) 

关于python - 如何使用sqlalchemy调用存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33306647/

相关文章:

php - 如何在 Codeigniter Active 记录中编写 MYSQL 查询 !=?

php - 这个循环有什么问题?

Python Google App Engine 无法释放对象数组的内存

java - 如何创建简单的 post/get API,使用 Google App Engine 简单调用 GAE 数据库?

python - python中使用cookielib模拟浏览器获取url

Python:将 csv 数据添加到列表并打印时如何删除引号

python - 类对象列表(鸟类)。每只鸟都有一种颜色。我如何最有效地获得一组所有颜色?

用于合并 DXF 文件的 Python 模块

mysql - 在Mysql中设置空间精度

Python 问题 - 我有一个类列表,如何删除重复项?