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

时间:2013-10-14 20:23:05

标签: python mysql python-2.7

我有以下代码,我一次运行,然后在插入新值进行刷新时再次运行。

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

问题是,在我使用以下代码将值插入'additive'表后,我得到旧值(缺少新值)。

def createAdditive(self, name):
        additiveInsertQuery = """ INSERT INTO additives
                                  SET         name = '""" + name + """'"""
        try:
            self.cursor.execute(additiveInsertQuery)
            self.db.commit()
            return "True"
        except:
            self.db.rollback()
            return "False"

我可以通过查看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()

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

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

1 个答案:

答案 0 :(得分:1)

对于那些发现这个问题的人:在Why are some mysql connections selecting old data the mysql database after a delete + insert?

中解决了类似的问题

诀窍是添加connection.commit()。