将web2py DAL与临时表一起使用

时间:2012-08-25 16:21:36

标签: python data-access-layer web2py

我正在尝试通过web2py DAL对临时表执行一些原始SQL,但我的结果都返回无。

这是完整的功能:

def test():

    db_test = DAL('mysql://root:root@localhost/test')

    sql = """CREATE TEMPORARY TABLE tmp LIKE people;
    INSERT INTO tmp SELECT * FROM people;
    INSERT INTO tmp SELECT * FROM people;
    SELECT * FROM tmp;"""

    results = db_test.executesql(sql)

显然,SQL是一种简化,但在SQL窗格中运行相同的SQL会返回正确的结果。我需要做些什么才能让DAL使用它?

1 个答案:

答案 0 :(得分:1)

您无法在一个executesql来电中执行多个语句我怀疑; web2py使用DBAPI 2.0 .execute()调用将这些调用发送到后端数据库,并且通常只支持单个语句:

db_test = DAL('mysql://root:root@localhost/test')

sqlddl = """CREATE TEMPORARY TABLE tmp LIKE people;
INSERT INTO tmp SELECT * FROM people;
INSERT INTO tmp SELECT * FROM people;"""

for statement in sqlddl.split(';'):
    db_test.executesql(statement.strip())

sqlselect = "SELECT * FROM tmp;"
results = db_test.executesql(sqlselect)