数据库查询包含旧数据

问题描述:

你好,


我在使用MySQL时遇到Python db api的问题。


我已经使用wxPython编写了一个带有GUI的程序,在main.py中包含了

,它导入另一个模块 - reports.py。使用gui选择了几个报告

,每个报告都是report.py文件中的一个

类。这些类包含一个方法,即
将数据作为参数传递并生成报告,将HTML写入文件

并使用matplotlib创建图形文件。


使用以下

代码从GUI调用报告类方法:

agentlist = self.getselected()

detail = self.cb1.GetValue()

if self.getdates()!= False:

fromdate,todate = self.getdates()

app.mainframe.SetStatusText(" Generating Report ...")

if self.reportchoice.GetSelection()== 0:

thereport = reports.VehicleRunningCost()

thereport.producereport(agentlist,fromdate = fromdate,

todate = todate,detail = detail)

app。 mainframe.SetStatusText(" Report Complete")

viewerframe = ViewerFrame(无,-1,里程和燃料

报告查看器)

viewerframe.Show(True)


第一次运行报告时,所有内容按预期工作但是如果你再次运行它,那么在修改数据之后,似乎在第二次报告运行时选择了修改前的数据

。 br />

如果有人能给我任何关于

为什么选择旧数据的提示,将不胜感激。我没有发布完整的代码因为

它很长但如果有人想看到特定的

部件会发布更多。


谢谢!

Andrew

Hi there,

I''m having a problem with the Python db api, using MySQL.

I''ve written a program with a GUI using wxPython, the GUI is contained
in main.py which imports another module - reports.py. There are
several reports that are selected using the gui, and each report is a
class in the file reports.py. These classes contain a method which is
passed data as arguments and produces a report, writing HTML to a file
and using matplotlib to create a graph file.

The report class methods are called from the GUI using the following
code:
agentlist = self.getselected()
detail = self.cb1.GetValue()
if self.getdates() != False:
fromdate, todate = self.getdates()
app.mainframe.SetStatusText("Generating Report...")
if self.reportchoice.GetSelection() == 0:
thereport = reports.VehicleRunningCost()
thereport.producereport(agentlist, fromdate=fromdate,
todate=todate, detail=detail)
app.mainframe.SetStatusText("Report Complete")
viewerframe = ViewerFrame(None, -1, "Mileage and Fuel
Report Viewer")
viewerframe.Show(True)

The first time you run a report, everything works as expected but if
you run it a second time, after modifying data, it seems that the data
from before the modification is selected on the second report run.

It would be much appreciated if anyone could give me any hints as to
why the old data is selected. I haven''t posted the full code because
it''s quite long but will post more if anyone wants to see specific
parts.

Thanks!
Andrew

2008年5月21日星期三上午6:30 ,< gi ******** @ gmail.comwrote:
On Wed, May 21, 2008 at 6:30 AM, <gi********@gmail.comwrote:

第一次运行报告时,一切都按预期工作但是如果

你再次运行它,在修改数据之后,似乎在第二次报告运行时选择了修改前的数据


The first time you run a report, everything works as expected but if
you run it a second time, after modifying data, it seems that the data
from before the modification is selected on the second report run.



您是否记得在重新运行报告之前提交更改?

Python的DB API要求任何自动提交默认情况下,

底层数据库的功能将被关闭,因此您需要自己进行
提交更改。如果你习惯让自动提交转为

,这可能会令人困惑。


参见 http://www.python.org/dev/peps/pep-0249/ 了解更多详情

python'的DB API。


-

Jerry

Did you remember to commit your changes before re-running the report?
Python''s DB API requires that any auto-commit feature of the
underlying database be turned off by default, so you are required to
commit changes yourself. If you''re used to having auto-commit turned
on, this can be confusing.

See http://www.python.org/dev/peps/pep-0249/ for more details of
python''s DB API.

--
Jerry

>

On 21 Mai,15:22,giraffe ... @ gmail.com写道:
On 21 Mai, 15:22, giraffe...@gmail.com wrote:

>

我做了我通过修改数据确认了这一点,从mysql命令行客户端选择它来验证更改,然后再次运行

报告。如果我退出应用程序然后再次启动它,

一切都按预期工作,直到报告的第二个实例运行


>
I did and I confirmed this by modifying the data, selecting it from
the mysql command line client to verify the changes, then running the
report again. If I exit the application and then start it again,
everything works as expected until the second instance of the report
is run.



请注意,如果您的程序中已打开连接,特别是如果

该连接已被用于选择数据,则可能是

的情况,然后你必须在尝试访问新添加的数据之前执行回滚或提交。这种行为的原因

是DB-API模块将代表您的

开始交易,并且当该交易开放时,在其他交易中提交的更改/>
交易可能对您自己的交易无法使用,具体取决于交易隔离级别

.


MySQL似乎使用可重复读取 ;默认情况下[1]为

事务隔离级别,而PostgreSQL(例如)使用

read committed默认情况下[2]。我猜想如果你使用的是
PostgreSQL,那么这个特殊的问题就不会发生了,但是

是了解长时间效果的其他原因。 PostgreSQL中的
事务,以及定期执行回滚的b
的做法仍然值得考虑使用

数据库系统。


Paul


[1] http://dev.mysql.com/doc/refman/5.1/...isolation.html

[2 ] http://www.postgresql.org/docs /8.1/i...ction-iso.html


5月21日下午3:23,Paul Boddie< p ... @ boddie .org.ukwrote:
On May 21, 3:23 pm, Paul Boddie <p...@boddie.org.ukwrote:

请注意,如果你的程序中有一个连接打开,特别是如果

那个c onnection已被用于选择数据,它可能是

的情况,然后你必须在尝试访问新添加的数据之前执行回滚或提交。这种行为的原因

是DB-API模块将代表您的

开始交易,并且当该交易开放时,在其他交易中提交的更改/>
交易可能对您自己的交易不可用,具体取决于

事务隔离级别。
Note that if you have a connection open in your program, especially if
that connection has already been used to select data, it may be the
case that you then have to perform a rollback or commit before
attempting to access newly added data. The reason for this behaviour
is that the DB-API modules will have begun a transaction on your
behalf, and while that transaction is open, changes committed in other
transactions may be unavailable to your own transaction, depending on
the transaction isolation level.



感谢Paul,似乎已经完美地解决了这个问题。我总是只想到查询一个数据库总会给你

最新的数据,猜猜它只是表明事情永远不会像

首次出现时很简单!

Thanks for that Paul, seems to have solved the problem perfectly. I
had always just thought querying a database would always give you the
most current data, guess it just goes to show that things are never as
simple as they first appear!