From 0780c83c358da230d7e776b52de3d1bb2565e689 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Thu, 13 Aug 2020 11:07:07 +0100 Subject: [PATCH] Turning insert statement into f string instead of using + operator to join string --- main.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 1a77c08f..d6a1a314 100644 --- a/main.py +++ b/main.py @@ -230,6 +230,9 @@ async def on_guild_join(guild): # Setup pool pool = client.db + # Grabbing the values to be inserted + records = ", ".join(map(lambda m: f"({guild.id}, {m.id})", guild.members)) + # Setup up pool connection and cursor async with pool.acquire() as conn: async with conn.cursor() as cur: @@ -244,9 +247,8 @@ async def on_guild_join(guild): async with conn.cursor() as cur: # Define the insert statement that will insert the user's information - insert = """INSERT INTO members (guildID, discordID) VALUES""" + ", ".join( - map(lambda m: f"({guild.id}, {m.id})", - guild.members)) + """ ON DUPLICATE KEY UPDATE guildID = VALUES(guildID), discordID = VALUES(discordID)""" + insert = f"""INSERT INTO members (guildID, discordID) VALUES {records} + ON DUPLICATE KEY UPDATE guildID = VALUES(guildID), discordID = VALUES(discordID)""" # Execute the query await cur.execute(insert)