当插入新值以刷新时,我会运行以下代码。在
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
对于那些发现这个问题的人:类似的问题在Why are some mysql connections selecting old data the mysql database after a delete + insert?中得到了解决
诀窍是添加一个连接.提交(). 在
相关问题 更多 >
编程相关推荐