Python 从数据库中检索更新的表-MySQLdb

Python 从数据库中检索更新的表-MySQLdb,python,mysql,mysql-python,Python,Mysql,Mysql Python,我试图仅使用一个连接从表中检索所有数据。下面是一个伪代码: import MySQLdb db = MySQLdb.connect(host,user,pass,db) def some_fun(): global db cur=db.cursor() cur.execute("SELECT * FROM TableName") result = cur.fetchall() for i in result: print i cu

我试图仅使用一个连接从表中检索所有数据。下面是一个伪代码:

import MySQLdb

db = MySQLdb.connect(host,user,pass,db)
def some_fun():
    global db
    cur=db.cursor()
    cur.execute("SELECT * FROM TableName")
    result = cur.fetchall()
    for i in result:
        print i
    cur.close()

while True:
    some_fun()
此代码不起作用,每次都会给出相同的记录,因此即使表的内容发生了更改

如何仅使用一个连接实现多个查询,而不在每次必须获取表内容时打开连接


谢谢

如果您主要关心的是在不重复关闭和打开连接的情况下重新使用连接,您可以执行以下操作

光标不会关闭,每个新查询都将获取表中的更新数据

import MySQLdb

class Dbconnect(object):
    def __init__(self):
        self.dbconection = MySQLdb.connect(host='host', port=int('port'), user='user', passwd'passwd', db='schema_db')
        self.dbcursor = self.dbconection.cursor()


db = Dbconnect()
# conection made before the infinite loop
while True:
    db.dbcursor.execute()
    # use an iterator to view results. This consumes the cursor
    for row in db.dbcursor:
        print row

要从表中提取所有数据,请执行(“从表名中选择*)这部分代码已经执行了。while循环是不必要的。但是还有一些其他代码将数据插入到同一个表中。。因此,我的这个while循环获取更新的表