简体   繁体   English

为什么下面的代码会抛出 0 异常?

[英]Why is the following code throwing an exception of 0?

I'm using mariadb 10, python, and pymysql to put together a register command for my discord server.我正在使用 mariadb 10、python 和 pymysql 为我的 discord 服务器组合一个注册命令。 In mariadb, I have a stored procedure that has the following syntax:在 mariadb 中,我有一个具有以下语法的存储过程:

DELIMITER //

CREATE PROCEDURE registerUser(IN iUser_Name varchar(50), OUT isExists bool)
BEGIN

    IF EXISTS (SELECT 1 FROM user where user_name = iUser_Name) THEN
        SET isExists = true;
    ELSE
        SET isExists = false;
        insert into user(user_name, user_create_date) VALUES(iUser_Name, curdate());
    END IF;
END //

DELIMITER ;

The goal of the stored procedure is to determine if the user exists, if yes, set isExists to true else false and insert the user.存储过程的目的是判断用户是否存在,如果存在,设置isExists为true,否则为false,插入用户。

In my python script, i have tried a variety of solutions based on what I found here on stackoverflow and tried to implement it but none work.在我的 python 脚本中,我根据我在 stackoverflow 上找到的内容尝试了各种解决方案,并尝试实现它,但没有任何效果。 The following is the latest attempt and I'm getting the error message: Something went wrong: 0以下是最新尝试,我收到错误消息: Something went wrong: 0

import pymysql.cursors
...

@client.command(name="register", pass_context=True)
async def register(ctx):
    #db connection
    cnx = pymysql.connect(user='db_user'
    , password = 'pwd'
    , host='localhost'
    , database='db_name'
    , port=3306
    , cursorclass=pymysql.cursors.DictCursor)

    cursor = cnx.cursor()

    try:
        # https://stackoverflow.com/questions/24139849/mysqldb-stored-procedure-out-parameter-not-working

        cursor.callproc('registerUser', (ctx.message.author.name, 'True'))
        cursor.execute("select @_registerUser_1")
        result = cursor.fetchone()

        if result[0] == False:
            await ctx.channel.send("{}, you are all set to weekly trade.".format(ctx.message.author.mention))
        elif result[0] == True:
            await ctx.channel.send("{}, you have already registered.".format(ctx.message.author.mention))

        cnx.commit()
        cursor.close()
        cnx.close()
    except Exception as err:
        await ctx.channel.send("Something went wrong: {}".format(err))

any suggestions to get this thing working would be greatly appreciate.任何让这件事工作的建议将不胜感激。

thanks.谢谢。

This documenation suggests that you simply use the value returned by the #fetchone() call, not try to index it like an array. 该文档建议您只需使用#fetchone()调用返回的值,而不是尝试像数组一样对其进行索引。 It seems you should be comparing if result['@_registerUser_1'] == False: , better yet if not result['@_registerUser_1']:看来你应该比较if result['@_registerUser_1'] == False:if not result['@_registerUser_1']:更好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM