I am creating an discord command called bancheck. The command takes an banid which is an integer stored in an mysql database. I then fetch the user_id and reason from the tables bans where the ban_id is. I then execute that but receive the error.
mysql.connector.errors.ProgrammingError: Could not process parameters: int(4), it must be of type list, tuple or dict
CODE:
@commands.slash_command(name="bancheck", description="Checks the database for an ban.")
async def bancheck(self, ctx, ban_id: int):
mydb = mysql.connector.connect(
host=config.host,
user=config.user,
password=config.password,
database=config.database
)
mycursor = mydb.cursor()
sql = "SELECT user_id, reason FROM bans WHERE ban_id = %s"
val = (ban_id)
mycursor.execute(sql, val)
result = mycursor.fetchone()
if result:
user_id, reason = result
message = f"The user with ID {user_id} was banned for '{reason}'"
else:
message = "No ban was found with that ID."
embed = discord.Embed(title="Ban Check", description=message, color=discord.Color.red())
embed.add_field(name="Ban ID", value=f"{ban_id}", inline=False)
await ctx.respond(embed=embed)
mydb.close()
The error occurs on line 63 code mycursor.execute(sql, val)
Here is the table structure, the ban_id is an auto increment value.
The error is as described; your val
needs to be a tuple, list, or dictionary. Wrapping a value in parethenses by itself won't make it a tuple - gotta chuck a comma in there as well. Without it, the parentheses get removed and it's interpreted as a single value rather than a sequence.
val = (ban_id, ) # will be interpreted as a tuple
mycursor.execute(sql, val)
Example in an ipython terminal: