MySQLdb.ProgrammingError: not all arguments converted during bytes formatting

I have several users in the database and all the ones with id < 10 work fine, but the ones with id > 10 no longer work. The error that I get in the console is:

Traceback (most recent call last):
  File "C:\Flask\APP-POWERBY\env\Lib\site-packages\MySQLdb\cursors.py", line 201, in execute
    query = query % args
            ^^^^^^^^^^^^
TypeError: not all arguments converted during bytes formatting

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Flask\APP-POWERBY\env\Lib\site-packages\flask\app.py", line 2213, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Flask\APP-POWERBY\env\Lib\site-packages\flask\app.py", line 2193, in wsgi_app
    response = self.handle_exception(e)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Flask\APP-POWERBY\env\Lib\site-packages\flask\app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Flask\APP-POWERBY\env\Lib\site-packages\flask\app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Flask\APP-POWERBY\env\Lib\site-packages\flask\app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Flask\APP-POWERBY\env\Lib\site-packages\flask\app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)

Something has to do with this def…

def obtener_report_por_id_user(db,id_user):
    with db.connection.cursor() as cursor:
        juegos = []
        #print(type(id))
        #print(id)
        result = cursor.execute("SELECT id,name,report FROM reports_workspace WHERE id_user = %s", str(id_user))
        juegos = cursor.fetchall()
        #print(juegos)
    return juegos

Can you help me?
Thank you.

Fix the problem by modifying the mysql query

cursor.execute("""SELECT id,name,report FROM reports_workspace WHERE id_user = '{}'""".format(id_user))

This is NOT a solution, this is a much much worse problem! Don’t ever do this.

Check the documentation for the execute() method. What is the intended way to pass arguments to the query? Have a look at it, and imagine what you would do if you had two or more arguments instead of just one.

I don’t quite understand what you’re saying…I previously had the following

cursor.execute("SELECT id,name,report FROM reports_workspace WHERE id_user = %s", str(id_user))

but for some reason it gave me an error if the user id is greater than 9

Yes. Did you check the documentation? A few seconds with Google turned up this page, but you may have something else you’ve been reading. MySQLdb User’s Guide — MySQLdb 1.2.4b4 documentation What does it say about the correct way to pass arguments?

I see some way but it has not worked for me… which one should I use correctly?

Use the one in the documentation.

Do you mean this (example)?

c.execute("""SELECT spam, eggs, sausage FROM breakfast
          WHERE price < %s""", (max_price,))

This way it doesn’t work for me with id’s greater than 9

result = cursor.execute("""SELECT id,name,report,role,descripcion FROM reports_workspace WHERE id_user = %s""", str(id_user))

Exactly. Compare the example, which works perfectly, with your one that doesn’t.

I’m sorry but I don’t see what’s wrong…

c.execute("""SELECT spam, eggs, sausage FROM breakfast
          WHERE price < %s""", (max_price,))
cursor.execute("""SELECT id,name,report,role,descripcion FROM reports_workspace WHERE id_user = %s""", str(id_user))

with str(id_user) it also gives me an error.

Do you see a difference? (max_price,) works and str(id_user) doesn’t. If you don’t see a difference, read the documentation some more. I don’t think there’s anything more I can say to make this clearer. There is a highly significant difference here and you MUST see it or your databasing work will forever fail.

there is mother!
she didn’t see the comma. Sorry, now it works ok.

Thank you so much!

Excellent :slight_smile: With a bit of thought, you can probably understand exactly why your original code failed once you hit a second digit. Or don’t worry about it, and just make the version with the tuple.