Home > OS >  SQLITE database update request don't work
SQLITE database update request don't work

Time:12-16

I'have a bot and i've made a loop for check if the server has made change like name or number of member but the request don't update my database.

I don't have any error message. And to try if the request is send i had 2 print and in terminal "hello" appear but my database isn't update.

The code:

Main.py:

@bot.event
async def on_ready() -> None:
    print ("Bot is ready !")
    print (f"Logged into {bot.user.name}")
    print (f"ID : {bot.user.id}")
    get_serv_info.start()

@tasks.loop()
async def get_serv_info() -> None:
    for server in bot.guilds:

        server_name = server.name
        server_id = server.id
        nb_member = server.member_count
    try:
        database_handler.update_server(server_id, server_name, nb_member)
        print("hello")
    except:
        print("hi")

    await asyncio.sleep(10)

database_handler.py:

def update_server(self, server_name : str, server_id : str, nb_member : str):
        cursor = self.connect.cursor()
        query = f"UPDATE Servers SET server_name = ?, nb_member = ? WHERE server_id = ?;"
        cursor.execute(query, (server_name, nb_member, server_id))
        cursor.close()
        self.connect.commit()

CodePudding user response:

Based on their names, it looks like you're passing parameters in the wrong order. That likely means you'll either get no matches on server_id, or you'll be changing your data in unexpected ways.

Swap server_id and nb_member so they match the order in the query:

query = f"UPDATE Servers SET server_name = ?, nb_member = ? WHERE server_id = ?;"
cursor.execute(
    query, (
        server_name,  # First parameter
        nb_member,    # Second parameter
        server_id,    # Third parameter
    )
)

Alternatively, you can use named placeholders by passing in a dict:

query = """UPDATE Servers
           SET server_name = :server_name,
               nb_member = :nb_member
           WHERE server_id = :server_id;"""

cursor.execute(
    query,
    {
        "server_name": server_name,
        "server_id": server_id,
        "nb_member": nb_member,
    }
)

This lets you pass parameters in in any order. It can also help with clarity, especially when you're dealing with more than a small number of parameters.

CodePudding user response:

I try this:

try:
  database_handler.create_server(server_id, server_name, nb_member)
  print("hello")
except:
  pass

It print hello

And this:

try:
  database_handler.create_server(server_id, server_name, nb_member)
except:
  print("hello")

And it print nothing

  • Related