# MySQLdb.ProgrammingError: not all arguments converted during bytes formatting

**URL:** <https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476>\
**Category:** Python Help\
**Created:** [June 7, 2023, 6:03am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476 "2023-06-07T06:03:25Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/peya02/32/12515_2.png) [@peya02](https://discuss.python.org/u/peya02)\
**Post date:** [June 7, 2023, 6:03am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/1 "2023-06-07T06:03:25Z")

</div>

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:

```auto
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…

```auto
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.

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/peya02/32/12515_2.png) [@peya02](https://discuss.python.org/u/peya02)\
**Post date:** [June 7, 2023, 9:26am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/2 "2023-06-07T09:26:50Z")

</div>

Fix the problem by modifying the mysql query

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

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [June 7, 2023, 11:16am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/3 "2023-06-07T11:16:22Z")

</div>

> [@peya02](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/peya02/32/12515_2.png) [@peya02](https://discuss.python.org/u/peya02)\
**Post date:** [June 7, 2023, 12:19pm UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/4 "2023-06-07T12:19:05Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [June 7, 2023, 12:23pm UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/5 "2023-06-07T12:23:40Z")

</div>

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](https://mysqlclient.readthedocs.io/user_guide.html#some-examples) What does it say about the correct way to pass arguments?

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/peya02/32/12515_2.png) [@peya02](https://discuss.python.org/u/peya02)\
**Post date:** [June 8, 2023, 7:15am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/6 "2023-06-08T07:15:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [June 8, 2023, 7:16am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/7 "2023-06-08T07:16:43Z")

</div>

> [@peya02](#):
>
> which one should I use correctly?

Use the one in the documentation.

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/peya02/32/12515_2.png) [@peya02](https://discuss.python.org/u/peya02)\
**Post date:** [June 8, 2023, 7:56am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/8 "2023-06-08T07:56:49Z")

</div>

Do you mean this (example)?

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

```

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/peya02/32/12515_2.png) [@peya02](https://discuss.python.org/u/peya02)\
**Post date:** [June 8, 2023, 8:01am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/9 "2023-06-08T08:01:24Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [June 8, 2023, 8:09am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/10 "2023-06-08T08:09:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/peya02/32/12515_2.png) [@peya02](https://discuss.python.org/u/peya02)\
**Post date:** [June 8, 2023, 9:03am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/11 "2023-06-08T09:03:05Z")

</div>

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

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

```

```auto
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.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [June 8, 2023, 9:10am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/12 "2023-06-08T09:10:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![peya02](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/peya02/32/12515_2.png) [@peya02](https://discuss.python.org/u/peya02)\
**Post date:** [June 8, 2023, 9:14am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/13 "2023-06-08T09:14:33Z")

</div>

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

Thank you so much!

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [June 8, 2023, 9:31am UTC](https://discuss.python.org/t/mysqldb-programmingerror-not-all-arguments-converted-during-bytes-formatting/27476/14 "2023-06-08T09:31:39Z")

</div>

Excellent 🙂 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.
