推荐学习书目
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
hhp
V2EX  ›  Python

连接 MySQL 报错'NoneType' object has no attribute '__getitem__'

  •  
  •   hhp · Jul 14, 2016 · 9887 views
    This topic created in 3590 days ago, the information mentioned may be changed or developed.
    新手一枚,想批量获取一批数据在后台自动生成的 ID 然后做匹配
    本来是 for 循环,报错后拆开来还是报错那应该不是我循环的问题了
    第一段能正常获取到 ID ,为什么第二段始终返回 NONE

    Traceback (most recent call last):
    File "add_store.py", line 119, in <module>
    main()
    File "add_store.py", line 113, in main
    print result[0]
    TypeError: 'NoneType' object has no attribute '__getitem__'

    代码如下:
    (删掉了括号里的敏感信息)
    hosturl =
    posturl =
    posturl2 =
    cj = cookielib.LWPCookieJar()
    cookie_support = urllib2.HTTPCookieProcessor(cj)
    opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
    urllib2.install_opener(opener)
    h = urllib2.urlopen(hosturl)
    headers = {}
    postData = {}
    postData = urllib.urlencode(postData)
    request = urllib2.Request(posturl, postData, headers)
    response = urllib2.urlopen(request)

    conn = MySQLdb.connect()

    def main():
    i = 31
    stname = 'teststore' + str(i)
    postData2 = {'storeName': stname}
    postData2 = urllib.urlencode(postData2)
    request2 = urllib2.Request(posturl2, postData2, headers)
    response2 = urllib2.urlopen(request2)
    cur = conn.cursor()
    cur.execute("select a.store_id from store as a where a.store_name = %s", [stname])
    print stname
    result = cur.fetchone()
    print result[0]

    i = 32
    stname = 'teststore' + str(i)
    postData2 = {'storeName': stname}
    postData2 = urllib.urlencode(postData2)
    request2 = urllib2.Request(posturl2, postData2, headers)
    response2 = urllib2.urlopen(request2)
    cur.execute("select a.store_id from store as a where a.store_name = %s", [stname])
    print stname
    result = cur.fetchone()
    print result[0]
    cur.close()

    conn.close()

    if __name__ == '__main__':
    main()
    Supplement 1  ·  Jul 14, 2016
    我擦……解决了……要加 commit ……为什么 mysql 的 select 也要 commit ……
    11 replies    2016-07-14 17:27:23 +08:00
    hhp
        1
    hhp  
    OP
       Jul 14, 2016 via iPhone
    有大神知道为什么吗(星星眼
    myyou
        2
    myyou  
       Jul 14, 2016
    你 result 返回的是 None ,所以 result [0] 取不了值
    hhp
        3
    hhp  
    OP
       Jul 14, 2016 via iPhone
    @myyou 对,我就是在问为什么返回 none 了,第一段里都是正常的,第二段 sql 在数据库里也是有记录的
    myyou
        4
    myyou  
       Jul 14, 2016
    @hhp 你 store_name 应该没有叫 teststore32 的吧
    hhp
        5
    hhp  
    OP
       Jul 14, 2016 via iPhone
    @myyou 有的,查了,是成功写到表里了
    上面的 31 就有,两段代码是一样的
    hhp
        6
    hhp  
    OP
       Jul 14, 2016 via iPhone
    @myyou 我总觉得是 fetchone 的问题,但网上查了半天都没找到确切原因
    dozer47528
        7
    dozer47528  
       Jul 14, 2016
    @hhp 你能看一下两次 execute 的返回值是什么吗?
    我和你有类似的问题,未解决: http://stackoverflow.com/questions/38241440/python-mysqldb-execute-result-doesnt-match-fetch-result
    dozer47528
        8
    dozer47528  
       Jul 14, 2016
    @hhp 我是用了连接池后出现问题的,而你的代码里多次查询用了同一个连接,所以其实和我情况类似。
    dozer47528
        9
    dozer47528  
       Jul 14, 2016
    @hhp 可以加我个QQ 41709580
    hhp
        10
    hhp  
    OP
       Jul 14, 2016 via iPhone
    @dozer47528 第一次是 1 第二次是 0 ,但是数据库里实际上是有查询结果的
    dozer47528
        11
    dozer47528  
       Jul 14, 2016
    @hhp 那我和你不一样,我 2 次都是 1 ,但 fetchone 确是 None
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3422 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 10:53 · PVG 18:53 · LAX 03:53 · JFK 06:53
    ♥ Do have faith in what you're doing.