【问题标题】:why cursor.execute with pymysql always get nonetype error为什么用pymysql执行cursor.execute总是得到nonetype错误
【发布时间】:2015-11-12 12:45:53
【问题描述】:

我试图在数据库中使用 cursor.execute 更新记录,但它总是返回 NoneType not iterable 错误,所以我决定使用类似“select curtime()”之类的简单方法进行测试,它给出了相同的错误。

cnx=pymysql.connect(user=user,password=pw,
                   host=127.0.0.1,port=3306,db=test,autocommit=True)
cursor=cnx.cursor()
cursor.execute('Select curtime()')

我得到'TypeError:'NoneType'对象不可迭代'

编辑: 我明白你需要完整的回溯是什么意思,我回到家,问题就消失了

File "/opt/anaconda3/lib/python3.4/site-packages/pymysql/cursors.py", line 51, in close
while self.nextset():

我从其他文章中发现了这个错误,因为我之前的真实 sql 语句字符串 cursor.execute(longsqlstatement) 很长,当我尝试关闭连接和游标时,它会导致该错误.在我的 curtime() 测试中,因为我之前的游标还没有完成执行它也会导致 NoneType 错误。所以我的问题是我怎么知道所有的 sql 语句已经成功运行并且我可以关闭连接?

【问题讨论】:

  • 不是来自你没有的代码。请显示完整的回溯。

标签: python cursor pymysql


【解决方案1】:

找不到答案,所以决定打破长长的多语句字符串 成功项目=0 失败项目=0

cursor = cnx.cursor()
executeitems=statement.split(';')
for item in executeitems:
    item=item.strip()
    if item!='':
        try:
            cursor.execute(item)
            result=cursor.rowcount
            if result>-1:
                #print('Successful:',item)
                successitem=successitem+1
        except:
            faileditem=faileditem+1
            print('--FAIL TO UPDATE:', item)

cnx.commit()
print('successfully commited items:',successitem)
print('failed items:',faileditem)

cnx.close()

【讨论】:

    猜你喜欢
    • 2018-09-06
    • 1970-01-01
    • 2016-07-08
    • 2014-11-21
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 2021-07-03
    相关资源
    最近更新 更多