【python】cursor.fetchall()操作后导致的No result set to fetch from
2018-02-19 02:57
459 查看
新人学习python。学习慕课网的python操作mysql课程。https://www.imooc.com/video/9219
然后进行都最后发现,卡在了扣钱上面,会显示No result set to fetch from。
以下是源码:# -$- coding:utf-8 -*-
"""
@author:Zhang
@file:transfer_money.py
@time:2018-02-19 1:48
"""
import sys
import mysql.connector
class TransferMoney(object):
def __init__(self,conn):
self.conn = conn
def check_acct_available(self,accid):
try:
cursor = self.conn.cursor()
sql = "SELECT * FROM account WHERE accid=%s" % accid
cursor.execute(sql)
print("check_acct_available:" + sql)
rs = cursor.fetchall()
if len(rs)!=1:
raise Exception("账号%s不存在" % accid)
finally:
cursor.close()
def has_enough_money(self,accid,money):
try:
cursor = self.conn.cursor()
sql = "SELECT * FROM account WHERE accid=%s and money>%s" % (accid,money)
cursor.execute(sql)
print("has_enough_money:" + sql)
rs = cursor.fetchall()
if len(rs) != 1:
raise Exception("账号%s没有足够钱" % accid)
finally:
cursor.close()
def redue_money(self,accid,money):
try:
cursor = self.conn.cursor()
sql = "UPDATE account SET money=money-%s WHERE accid=%s" % (money,accid)
cursor.execute(sql)
print("reduce_money:" + sql)
rs = cursor.fetchall()
if cursor.rowcount != 1:
raise Exception("账号%s减款失败" % accid)
finally:
cursor.close()
def add_money(self,accid,money):
try:
cursor = self.conn.cursor()
sql = "UPDATE account SET money=money+%s WHERE accid=%s" % (money,accid)
cursor.execute(sql)
print("add_money:" + sql)
rs = cursor.fetchall()
if cursor.rowcount != 1:
raise Exception("账号%s加款失败" % accid)
finally:
cursor.close()
def transfer(self,souce_accid,target_accid,money):
try:
self.check_acct_available(source_accid)
self.check_acct_available(target_accid)
self.has_enough_money(source_accid,money)
# 以上检验都成功的话
self.redue_money(source_accid,money)
self.add_money(target_accid,money)
self.conn.commit()
except Exception as e:
self.conn.rollback()
raise e
if __name__ == "__main__":
source_accid = sys.argv[1]
target_accid = sys.argv[2]
money = sys.argv[3]
conn = mysql.connector.connect(host="127.0.0.1",user="root",password="password",port=3306,db="test")
tr_money = TransferMoney(conn)
try:
tr_money.transfer(source_accid,target_accid,money)
except Exception as e:
print("出现问题:"+str(e))
finally:
conn.close()
我发现问题在于,是python操作mysql抛出unread result from。
然后我就先把rs = cursor.fetchall()给注释掉..或者改成 cursor.fetchone() 就能顺利运行了。
还不知道本质原理,但是方法先放着。弄清楚了回来补上。
然后进行都最后发现,卡在了扣钱上面,会显示No result set to fetch from。
以下是源码:# -$- coding:utf-8 -*-
"""
@author:Zhang
@file:transfer_money.py
@time:2018-02-19 1:48
"""
import sys
import mysql.connector
class TransferMoney(object):
def __init__(self,conn):
self.conn = conn
def check_acct_available(self,accid):
try:
cursor = self.conn.cursor()
sql = "SELECT * FROM account WHERE accid=%s" % accid
cursor.execute(sql)
print("check_acct_available:" + sql)
rs = cursor.fetchall()
if len(rs)!=1:
raise Exception("账号%s不存在" % accid)
finally:
cursor.close()
def has_enough_money(self,accid,money):
try:
cursor = self.conn.cursor()
sql = "SELECT * FROM account WHERE accid=%s and money>%s" % (accid,money)
cursor.execute(sql)
print("has_enough_money:" + sql)
rs = cursor.fetchall()
if len(rs) != 1:
raise Exception("账号%s没有足够钱" % accid)
finally:
cursor.close()
def redue_money(self,accid,money):
try:
cursor = self.conn.cursor()
sql = "UPDATE account SET money=money-%s WHERE accid=%s" % (money,accid)
cursor.execute(sql)
print("reduce_money:" + sql)
rs = cursor.fetchall()
if cursor.rowcount != 1:
raise Exception("账号%s减款失败" % accid)
finally:
cursor.close()
def add_money(self,accid,money):
try:
cursor = self.conn.cursor()
sql = "UPDATE account SET money=money+%s WHERE accid=%s" % (money,accid)
cursor.execute(sql)
print("add_money:" + sql)
rs = cursor.fetchall()
if cursor.rowcount != 1:
raise Exception("账号%s加款失败" % accid)
finally:
cursor.close()
def transfer(self,souce_accid,target_accid,money):
try:
self.check_acct_available(source_accid)
self.check_acct_available(target_accid)
self.has_enough_money(source_accid,money)
# 以上检验都成功的话
self.redue_money(source_accid,money)
self.add_money(target_accid,money)
self.conn.commit()
except Exception as e:
self.conn.rollback()
raise e
if __name__ == "__main__":
source_accid = sys.argv[1]
target_accid = sys.argv[2]
money = sys.argv[3]
conn = mysql.connector.connect(host="127.0.0.1",user="root",password="password",port=3306,db="test")
tr_money = TransferMoney(conn)
try:
tr_money.transfer(source_accid,target_accid,money)
except Exception as e:
print("出现问题:"+str(e))
finally:
conn.close()
我发现问题在于,是python操作mysql抛出unread result from。
然后我就先把rs = cursor.fetchall()给注释掉..或者改成 cursor.fetchone() 就能顺利运行了。
还不知道本质原理,但是方法先放着。弄清楚了回来补上。
相关文章推荐
- windows7下python2.6 + mysql5.5(No module named MySQLdb/DLL load failed/from sets import ImmutableSet)
- JDBC操作MySQL出现:This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, ...的问题解决
- MySQL必知必会 ERROR 1415 (0A000): Not allowed to return a result set from a trigger
- mysql error -- 1415 not allowed to return a result set from a trigger
- No way to set FetchSize to Spring SimpleJdbcCall
- Not allowed to return a result set from a trigger
- Beginning Python From Novice to Professional (3) - 列表操作
- Sql 2014 企业版 还原数据库时, 报错: 操作错误, 或 No backupset selected to be restored
- windows7下python2.6 + mysql5.5(No module named MySQLdb/DLL load failed/from sets import ImmutableSet)
- Fetch the result from result set
- MFC中剪贴板操作SetDataToClip()和GetDataFromClip()
- MongoDB数组更新操作$addToSet和$each修饰符
- ERROR manager.SqlManager: Error reading from database: java.sql.SQLException: Streaming result set
- python之cursor() — 数据库连接操作
- 【Python】selenium调用IE11浏览器,报错“找不到元素”NoSuchWindowException: Message:Unable to find element on closed window
- #713 – 在拖拽操作中使用控件预览图作为鼠标(Setting the Cursor to an Image of an UIElement While Dragging)
- 操作无效,原因是它导致对 SetCurrentCellAddressCore 函数的可重入调用。
- urllib2 - The Missing Manual HOWTO Fetch Internet Resources with Python
- Android中出现:Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 5 columns异常
- unity3D 彻底搞懂 Quaternion LookRotation FromToRotation SetLookRotation方法