为了账号安全,请及时绑定邮箱和手机立即绑定

出现问题:'str' object is not callable 为什么会这样啊,求解啊!!!

#coding:utf8
import sys
import pymysql
class TransferMoney(object):
    def __init__(self,conn):
        self.conn=conn
    def check_out_count(self,out_count):
        cursor = self.conn.cursor()
        try:
            sql='select * from new_table where id=%s'% out_count
            cursor.execute(sql)
            rs = cursor.fetchall()
            if len(rs)!=1:
                raise Exception("账户%s不存在"% out_count)
        finally:
            cursor.close()

    def check_in_count(self,in_count):
        cursor = self.conn.cursor()
        try:
            sql = 'select * from new_table where id=%s'% in_count
            cursor.execute(sql)
            rs = cursor.fetchall()
            if len(rs) != 1:
                raise Exception("账户%s不存在"% in_count)
        finally:
                cursor.close()

    def has_enough_money(self,out_count,money):
        cursor = self.conn.cursor()
        try:
            sql = 'select * from new_table where id=%d and money>%d'(out_count,money)
            cursor.execute(sql)
            print("have enough money:" + sql)
            rs = cursor.fetchall()
            if len(rs) != 1:
                raise Exception("账户%s没有足够的钱" % out_count)
        finally:
                cursor.close()


    def reduce_money(self,out_count,money):
        cursor = self.conn.cursor
        try:
            sql='update new_table set money=money-%s where id=%s'(money,out_count)
            cursor.execute(sql)
            rs = cursor.rowcount
            if len(rs) != 1:
                raise Exception("账户%s没有足够的钱" % out_count)
        finally:
            cursor.close()
    def add_money(self,in_count,money):
        cursor = self.conn.cursor
        try:

            sql = 'update new_table set money=money+%s where id=%s'(money, in_count)
            cursor.execute(sql)
            rs = cursor.rowcount
            if len(rs) != 1:
                raise Exception("账户%s没有足够的钱" % in_count)
        finally:
                cursor.close()
    def transfer(self,out_count,in_count,money):
        try:
            self.check_out_count(out_count)
            self.check_in_count(in_count)
            self.has_enough_money(out_count,money)
            self.reduce_money(out_count,money)
            self.add_money(in_count,money)
            self.conn.commit()
        except Exception as e:
            self.conn.rollback()
            raise e


if __name__=="__main__":
    out_count=sys.argv[1]
    in_count=sys.argv[2]
    money=sys.argv[3]
#创建于数据库连接
    conn=pymysql.connect(host='127.0.0.1',port =3306,user='root',password='password',db='test',charset='utf8')
    tr_money=TransferMoney(conn)
    try:
        tr_money.transfer(out_count,in_count,money)
    except Exception as e:
        print("出现问题:" + str(e))
    finally:
        conn.close()


正在回答

3 回答

reduce_money和add_money方法的if判断条件都写错了,if rs != 1就好,不用再len()了,因为已经是数字类型了

0 回复 有任何疑惑可以回复我~

还是不行啊,还是报一样的错误。。。

0 回复 有任何疑惑可以回复我~
def has_enough_money(self,out_count,money):
        cursor = self.conn.cursor()
        try:
            sql = 'select * from new_table where id=%d and money>%d'(out_count,money)
            cursor.execute(sql)
            print("have enough money:" + sql)
            rs = cursor.fetchall()
            if len(rs) != 1:
                raise Exception("账户%s没有足够的钱" % out_count)

这个sql语句换成%s

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Python操作MySQL数据库
  • 参与学习       85036    人
  • 解答问题       171    个

本视频教程讲解Python如何开发MySQL数据库程序

进入课程

出现问题:'str' object is not callable 为什么会这样啊,求解啊!!!

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信