V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
warcraft1236
V2EX  ›  Python

想 while 循环检查数据库字段的改变,发现不重新初始化数据库连接,不能查询到最新的数据库

  •  
  •   warcraft1236 · 2017-08-17 11:32:47 +08:00 · 2351 次点击
    这是一个创建于 2415 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我是用的 pymysql 这个库,大体的代码逻辑如下:

    ...
    做了一些事情,这些事情会导致数据库某个字段的值会改变
    ...
    
    a=usermysql() # 自定义类的 __init__方法,pymysql.connect 和创建 cursor
    while True:
    	a.execute() 
        result = a.fetchone()
        if result == 'WORKING': # 执行语句,获取需要的字段的值,如果值变成了想要的,就退出循环,做接下来的任务
        	break
    

    然而运行的时候,发现获取到的字段的值并不会变化,只有在 while 循环里初始化数据库的连接,才能获取到最新的数据库的值

    请问这是为什么呢?

    第 1 条附言  ·  2017-08-17 12:55:53 +08:00

    我创建的查询数据库的类是这样的

    class MySQLQuery():
        def __init__(self, ip, username, password, database):
            '''
            初始化 MySQL 数据库
            '''
            self.__sql = pymysql.connect(host=ip, user=username, password=password, db=database)
            self.__cursor = self.__sql.cursor()
    
        def selectOneResult(self, sentence):
            '''
            返回的结果是一个 tuple
            如果不存在,返回结果是个 NoneType
            '''
            self.__cursor.execute(sentence)
            result = self.__cursor.fetchone()
    
            return result
    

    初始化数据库连接是这样的

    from lib.util.MySQLQuery import MySQLQuery
    controllerDB = MySQLQuery(ip=MySQLIP, username=MySQLUserName, password=MySQLPassWord,
                              database='cloudplayer_controller_hometest')
    

    使用这个的是另一个文件,使用的部分是这样的

    from lib.util.GlobalVariable import controllerDB
    def a():  
        # 做了一些事情...
    
        while True:
            sqlSentence = 'select instance_id from service where id = {selectid}'.format(selectid=selectID)
            instanceID = controllerDB.selectOneResult(sqlSentence)[0]
    
            newSqlSentence = 'select status from instance_status where instance_id = {instanceid}'.format(instanceid=instanceID)
            status = controllerDB.selectOneResult(newSqlSentence)[0]
    
            if status == 'WORKING':
                break
            time.sleep(3)
    
    6 条回复    2017-08-17 13:24:16 +08:00
    siteshen
        1
    siteshen  
       2017-08-17 12:40:41 +08:00
    数据库配置记录所有的查询,查看数据库 log 看请求有没有被数据库处理。
    没有的话,查 execute 的文档,看是不是使用了类似执行过就不执行的机制。
    albertofwb
        2
    albertofwb  
       2017-08-17 12:40:52 +08:00 via Android
    仅仅从你 po 的这段 snippet 并不能看出问题,上源码链接吧
    warcraft1236
        3
    warcraft1236  
    OP
       2017-08-17 12:48:32 +08:00
    @albertofwb 理论上,同一个语句,不同时候执行,返回的结果应该都是数据库里边最新的数据是吗?
    QAPTEAWH
        4
    QAPTEAWH  
       2017-08-17 12:59:18 +08:00
    "MVCC"

    开启 autocommit,或者每次查完 commit。
    warcraft1236
        5
    warcraft1236  
    OP
       2017-08-17 13:03:53 +08:00
    @QAPTEAWH 能不能详细给讲一下呢?
    sujin190
        6
    sujin190  
       2017-08-17 13:24:16 +08:00
    pymysql 默认的 autocommit 是 false,不 commit 是不能查询到新的数据的,你可以设置为 true 或者每次查询完 commit
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3389 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:16 · PVG 19:16 · LAX 04:16 · JFK 07:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.