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
jookr
V2EX  ›  Python

求助 入门级简单地操作数据库的小问题

  •  
  •   jookr · 2014-12-24 17:35:41 +08:00 · 2483 次点击
    这是一个创建于 3404 天前的主题,其中的信息可能已经有所发展或是发生改变。
    循环更新数据库
    while里的第一次代码执行正常 到了第二次循环就会在 注释那行报错

    为什么到第二次循环就报错了呢 查了一下百度是不是关于游标什么,怎么解决呢

    谢谢各位

    # -*- coding: utf-8 -*-
    import hashlib,MySQLdb



    conn = MySQLdb.connect('localhost','root','123456','test',charset='utf8')
    cursor = conn.cursor()


    query = "select count(*) as count from `shujuku` where `puid`=''"
    cursor.execute(query)
    get_data = cursor.fetchone();
    total_data = int(get_data[0]);
    print 'total:',total_data;

    while(total_data >= 1):
    print total_data;
    query = '';
    query = "select `uid` from `shujuku` where `puid`='' order by `id` asc limit 10"
    #print query;
    cursor.execute(query);#为什么第二次循环 在这里报错呢?
    conn.commit();

    list_data = cursor.fetchall();


    query3 = '';
    for i in list_data:
    uid = str(i[0]);
    nuid = int(uid) - 1000000;
    nuid = str(nuid);
    md5 = hashlib.md5(nuid).hexdigest();
    #print(md5)
    puid = md5[0:2]+'/'+md5[-2:]+'/'+md5[2:-2];
    query3 = query3 +"update `shujuku` set `puid`='"+puid+"' where `uid`='"+uid+"';";

    cursor.execute(query3);
    #conn.commit();

    total_data = total_data - 10;

    print 'done';
    cursor.close()
    conn.close()
    5 条回复    2014-12-24 19:57:07 +08:00
    liprais
        1
    liprais  
       2014-12-24 17:56:02 +08:00
    报错信息是啥......你这样问问题问一半就是想帮你都帮不了啊.....
    jookr
        2
    jookr  
    OP
       2014-12-24 18:23:34 +08:00
    @liprais 报错信息如下

    Traceback (most recent call last):
    File "E:\python\tt.py", line 21, in <module>
    cursor.execute(query);
    File "D:\Python27\lib\site-packages\MySQLdb\cursors.py", line 202, in execute
    self.errorhandler(self, exc, value)
    File "D:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defau
    lterrorhandler
    raise errorclass, errorvalue
    _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run
    this command now")
    lixia625
        3
    lixia625  
       2014-12-24 18:49:37 +08:00
    要每次cursor close?
    lcqtdwj
        4
    lcqtdwj  
       2014-12-24 19:18:33 +08:00
    commit去掉试试,默认是自动提交,不需要你手动commit
    lcqtdwj
        5
    lcqtdwj  
       2014-12-24 19:57:07 +08:00
    @lcqtdwj Mysqldb默认是关的,搞错了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2872 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:27 · PVG 22:27 · LAX 07:27 · JFK 10:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.