从 SQLAlchemy 调用的 MySQL 过程每次调用都会返回旧结果

问题描述 投票:0回答:1

我有一些 MySQL 程序,当我直接在 MySQL 中运行它们时,它们可以正常工作。但是当我使用 SQLAlchemy 调用它们时,每次调用它们时,它们都会返回新结果以及上次调用的结果。例如,第一次调用返回四行。第二个调用应该返回五行,但它返回九行——五行加上前面的四行。这会无限期地持续下去。以下是其中一项程序:

DELIMITER $$
CREATE DEFINER=`service`@`localhost` PROCEDURE `sp_get_filtered_items`(IN theArray varchar(255), IN searchText varchar(500))
BEGIN
    drop table if exists tmpFilteredItems; 
    if length(theArray) = 0 then
        create temporary table tmpFilteredItems
        as 
        (select * from vw_items
        where alltext like CONCAT('%', searchText, '%'));
    else
        call sp_get_code_table(theArray); -- generates tmpCodeTable from array.
        create temporary table tmpFilteredItems
        as
        (select * from vw_items 
            inner join tmpCodeTable on vw_items.instanceof = tmpCodeTable.codeCol
            where alltext like CONCAT('%', searchText, '%'));
    end if;
    select * from tmpFilteredItems;
END$$
DELIMITER ;

这是我进行调用的 python 代码:

    def make_recordsets(self):
        raw_conn = self.eng.get_engine().raw_connection()

        try:
            # 1. create filtered results into temp table and retrieve
            cursor = raw_conn.cursor()
            cursor.callproc('sp_get_filtered_items', (self.instanceof_string, self.keyphrase,))
            for result in cursor.stored_results():
                tuples = result.fetchall()
                self.__items_to_dict(tuples)
            print(len(self.filtered_items))
            cursor.close()
            # 2. create properties list for follow-on searching; joins to temp table
            cursor = raw_conn.cursor()
            cursor.callproc('sp_get_filtered_props')
            for result in cursor.stored_results():
                tuples = result.fetchall()
                self.__props_to_dict(tuples)
            print(len(self.filtered_props))
            cursor.close()
            # 3. pick property with highest count for use in graphing link process.
            if not len(self.filtered_props) == 0:
                d = self.filtered_props[0] # get first row in rs, which has the highest count
                self.link_property = d['property']
                self.link_property_label = d['propertyLabel']
            # 4. create values list for use in graph creation; proc joins to temp table
            cursor = raw_conn.cursor()
            cursor.callproc('sp_get_filtered_values')
            for result in cursor.stored_results():
                tuples = result.fetchall()
                self.__values_to_dict(tuples)
            print(len(self.filtered_values))
            cursor.close()
            raw_conn.commit() # commit transactions to db
        except Exception as e:
            print(e.args)
            raw_conn.rollback()

        finally:
            raw_conn.close()
            self.eng.close()

请注意,我在此示例中处理了引擎连接 -

self.eng.close()
,尽管不建议这样做。只是想排除故障。为什么数据库会保留结果的历史记录并返回它们?

python mysql sqlalchemy
1个回答
0
投票

在这种情况下,我无意中保留了类属性中先前调用的状态,例如,

self.filtered_items
,这是一个Python列表。我使用 Flask 作为前端,它显然保留了我用来在 Web 浏览器的请求之间检索数据的类的状态。为了解决这个问题,我只是在重新填充之前清空属性中的列表:
self.filtered_items = []

© www.soinside.com 2019 - 2024. All rights reserved.