[SOLVED] How can i save a list into a mysql database?

Issue

I want to save a list with discord invites into my database. But everytime I ran this code, get’s only 1 item saved in the database. I tried to print the inviter ids to check if he just runs one item, but he runs the code for every item. So what is wrong? Why get’s only 1 item saved to the database?

            for invite in invites:

                if invite.inviter is None:
                    continue

                if not ctx.author.guild.get_member(int(invite.inviter.id)):
                    continue

                if int(invite.uses) == 0:
                    continue

                await cursor.execute("SELECT * FROM guild_invite_count WHERE guild_id = %s AND user_id = %s IS NOT NULL", (ctx.author.guild.id, invite.inviter.id))
                find_user = await cursor.fetchone()
                if find_user:
                    await cursor.execute("UPDATE guild_invite_count SET real_count = real_count + %s, total_count = total_count + %s WHERE guild_id = %s AND user_id = %s", (int(invite.uses), int(invite.uses), ctx.author.guild.id, invite.inviter.id))
                else:
                    await cursor.execute("INSERT INTO guild_invite_count (guild_id, user_id, real_count, total_count) VALUES (%s, %s, %s, %s)", (ctx.author.guild.id, invite.inviter.id, int(invite.uses), int(invite.uses)))
                print(invite.inviter.id)
                await mydb.commit()

Solution

This SQL

SELECT * FROM guild_invite_count WHERE guild_id = %s AND user_id = %s IS NOT NULL

is logically incorrect. AND user_id = %s IS NOT NULL reduces to AND user_id = true (unless the parameter is null). Just do

SELECT * FROM guild_invite_count WHERE guild_id = %s AND user_id = %s

Answered By – snakecharmerb

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.