4

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

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

问题是,在我使用以下代码将值插入“添加剂”表后,我得到了旧值(缺少新值)。

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
4

1 回答 1

2

对于那些发现这个问题的人:类似的问题在为什么有些mysql连接在删除+插入后选择mysql数据库中的旧数据?

诀窍是添加一个connection.commit()。

于 2015-12-19T23:54:03.917 回答