通过 sqlalchemy 执行时启用执行多条语句

2021-12-30 00:00:00 python sqlalchemy mysql

我有一个包含创建函数语句的 DDL 对象 (create_function_foo).在它的第一行我把 DROP FUNCTION IF EXISTS foo;engine.execute(create_function_foo) 返回:

I have a DDL object (create_function_foo) that contains a create function statement. In first line of it I put DROP FUNCTION IF EXISTS foo; but engine.execute(create_function_foo) returns:

sqlalchemy.exc.InterfaceError: (InterfaceError) Use multi=True when executing multiple statements

我把 multi=True 作为 create_engineengine.execute_optionsengine.execute 的参数,但它不起作用.

I put multi=True as parameter for create_engine, engine.execute_options and engine.execute but it doesn't work.

注意:engine 如果我的 create_engine

注意:我使用的是 python 3.2 + mysql.connector 1.0.12 + sqlalchemy 0.8.2

NOTE: I'm using python 3.2 + mysql.connector 1.0.12 + sqlalchemy 0.8.2

create_function_foo = DDL("""
DROP FUNCTION IF EXISTS foo;
CREATE FUNCTION `foo`(
    SID INT
) RETURNS double
READS SQL DATA
BEGIN
  ...
END
""")

我应该把它放在哪里?

推荐答案

multi=True 是 MySql 连接器的要求.您不能设置此标志将其传递给 SQLAlchemy 方法.这样做:

multi=True is a requirement for MySql connector. You can not set this flag passing it to SQLAlchemy methods. Do this:

conn  = session.connection().connection
cursor = conn.cursor()  # get mysql db-api cursor
cursor.execute(sql, multi=True)

此处的更多信息:http://www.mail-archive.com/sqlalchemy@googlegroups.com/msg30129.html

相关文章