简体   繁体   中英

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. In mariadb, I have a stored procedure that has the following syntax:

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.

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. The following is the latest attempt and I'm getting the error message: 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. It seems you should be comparing if result['@_registerUser_1'] == False: , better yet if not result['@_registerUser_1']:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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