cursor.fetchone()返回None,但是数据库中的行存在 [英] cursor.fetchone() returns None but row in the database exists

查看:1174
本文介绍了cursor.fetchone()返回None,但是数据库中的行存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用python写一个与MySQL数据库交互的程序. 对于sql查询,我使用MySQLdb. 问题是fetchone()返回None,但是在数据库浏览器中,我可以看到那个行存在. 这段代码:

I am writing a program on python which interacts with MySQL database. For sql queries I use MySQLdb. The problem is that fetchone() returns None but with the database browser I can see that that row exists. This piece of code:

query = "SELECT * FROM revision WHERE rev_id=%s;" 
cursor.execute(query % revision_id)
row = cursor.fetchone()
if row == None:
    raise Exception("there isn't revision with id %s" % revision_id)

我不知道这是怎么回事.有什么想法吗?

I have no idea what is going on here. Any ideas?

好吧,在某些情况下,它在某些情况下有效,但无论如何,当它 不起作用表中存在该行.我正在将游标对象传递给函数,并且上面的代码在函数中.问题与此光标对象有关.问题可能是我将游标作为参数传递给函数吗?我该如何测试?

okay, in some cases it works in some cases it doesn't but anyway when it does not work the row exists in the table. I am passing a cursor object to a function and the code above is in the function. The problem is connected with this cursor object. Could the problem be that I pass the cursor as an argument to the function? How can I test it?

是的,问题是我多次使用游标后,游标不起作用.因为其他程序连接到数据库,或者我做错了,所以会出错. 我有while循环,在该循环中我调用一个函数来从数据库获取信息.经过一些迭代后,它将不再起作用.还有另一个程序可以写入 while循环时DB起作用.

yes, the problem is that cursor does not work after I use it several times. Wether because other program connects to the DB or I am doing something wrong. I have while loop in which I call a function to get info from the DB. After some iterations it does not work again. There is another program which writes to the DB while while loop works.

推荐答案

好的,db.autocommit(True)解决了我的问题.

Okay, db.autocommit(True) solved my problem.

这篇关于cursor.fetchone()返回None,但是数据库中的行存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆