【python】cursor.fetchall()操作后导致的No result set to fetch from

新人学习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() 就能顺利运行了。

还不知道本质原理,但是方法先放着。弄清楚了回来补上。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值