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
1989922yan
V2EX  ›  Python

[问题]pyhotn - MySQLdb, 为什么MySQLdb的select值 始终不变?

  •  
  •   1989922yan · 2013-09-19 15:58:03 +08:00 · 3088 次点击
    这是一个创建于 3880 天前的主题,其中的信息可能已经有所发展或是发生改变。
    这是我在 python 中的操作:
    >>> import MySQLdb
    >>> connect = MySQLdb.connect(host='localhost',
    ... user='root',
    ... passwd='admin',
    ... charset='utf8',
    ... db='test',
    ... connect_timeout=365*24*3600)

    >>> cur = connect.cursor()
    >>> cur.execute('select max(Logid) from Log;')
    1L
    >>> cur.fetchone()
    (1044122L,)
    >>> cur.execute('select max(Logid) from Log;')
    1L
    >>> cur.fetchone()
    (1044122L,)

    —————————————————————————我是线—————————————————————————————

    这是我在mysql中的操作:
    mysql> select max(Logid) from Log;
    +------------+
    | max(Logid) |
    +------------+
    | 1044116 |
    +------------+
    1 row in set (0.00 sec)

    mysql> select max(Logid) from Log;
    +------------+
    | max(Logid) |
    +------------+
    | 1044123 |
    +------------+
    1 row in set (0.00 sec)


    ——————————————————————————我是线————————————————————————————

    我一开始注意到,fetchone()的值 与 mysql 不一致,
    一开始我以为是查询的时间差,
    值本身变了。

    后来,我发现,fetchone()的值,根本就不会变。
    有什么原因吗?
    是mysql的原因吗??
    还是 MySQLdb的原因???

    THX!!
    2 条回复    1970-01-01 08:00:00 +08:00
    VYSE
        1
    VYSE  
       2013-09-19 16:24:04 +08:00
    cursor.close()再新建查呢?
    hit9
        2
    hit9  
       2013-12-13 15:17:03 +08:00
    connect.autocommit(True) 试试,

    或者把每次查询放在事务之中
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   764 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:25 · PVG 06:25 · LAX 15:25 · JFK 18:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.