Python脚本返回旧的MySQL值(直到重新启动)

2021-04-12 00:27:50 发布

您现在位置:Python中文网/ 问答频道 /正文

当插入新值以刷新时,我会运行以下代码。在

def getStageAdditives(self, stage):
        stagesAdditivesSelectQuery = """SELECT      a.id,
                                                    a.name,
                                                    IFNULL(sa.dose, 0) as dose,
                                                    IFNULL(sa.last_dose, 0) as last
                                        FROM        additives a
                                        LEFT JOIN   stage_additives sa
                                        ON          a.id = sa.additive_id
                                        AND         sa.stage_id = (
                                        SELECT      id
                                        FROM        stages
                                        WHERE       name = '""" + stage + """')
                                        ORDER BY    a.name"""

        self.cursor.execute(stagesAdditivesSelectQuery)
        data = self.cursor.fetchall()

        additives = []
        for additive in data:
            id = additive[0]
            name = additive[1]
            dose = additive[2]
            last = additive[3]
            additives.append({ 'title': name, 'dose': dose, 'last': last })
        print stagesAdditivesSelectQuery
        return additives

问题是,在我使用下面的代码将一个值插入“addities”表后,我得到了旧值(缺少新值)。在

^{pr2}$

我可以通过查看phpMyAdmin来确认值被插入到数据库中。如果我重新启动脚本,我会得到预期的新值。如果我用phpMyAdmin运行查询,它也会返回新值。刷新页面并等待10秒以上并没有帮助,我仍然得到旧值。在

如果重要的话,这两种方法都在不同的类/文件中。“createAdditive”方法成功返回后,使用ajax调用getStageAdditives。在

数据库初始化:

import MySQLdb
import time

class Stage:
    def __init__(self):
        self.db = MySQLdb.connect('192.168.0.100', 'user', 'pass', 'dbname')
        self.cursor = self.db.cursor()

另一个检索相似值的方法按预期获取新值(与CreateAdditions相同的类):

def getAdditives(self, additive=None):
        where = ''
        if additive is not None:
            where = "WHERE pa.additive_id = ("
            where += "SELECT id FROM additives "
            where += "WHERE name = '" + additive + "') "
            where += "AND a.name = '" + additive + "'"

        additiveSelectQuery = """   SELECT      a.name,
                                                pa.pump_id
                                    FROM        additives a,
                                                pump_additives pa """ + where + """
                                    ORDER BY    a.name"""

        self.cursor.execute(additiveSelectQuery)
        data = self.cursor.fetchall()

        additives = []
        for item in data:
            additives.append( {'additive': item[0], 'pump': item[1]} )
        return additives