Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i save a list into a mysql database?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading