pythonmysqltypeerrorexecute

MySQL .execute TypeError: not all arguments converted during string formatting


Can anyone help what is the problem with this code?

image_url = "gs://{}/{}".format(bucket_name, image_name)
cursor.execute("SELECT image_url FROM image_db WHERE image_url = %s",(image_url,))

I am getting this error>

TypeError: not all arguments converted during string formatting

I tried formatting the string in different ways but it did not solve the issue.


Solution

  • I tested your problem in my setup and didn't run into the error you mentioned. Maybe there's a difference in our setups or in the database. Here's the code I used for testing (with Python version 3.11.5 and PyMySQL version 1.1.08) :

    bucket_name = 1
    image_name = "mmap"
    image_url = "gs://{}/{}".format(bucket_name, image_name)
    conn = pymysql.connect(host='192.168.110.247', port=3306, user='root', 
    password='root', database='matrixone')
    cursor = conn.cursor()
    cursor.execute("SELECT name FROM users WHERE name = %s", (image_url,))
    result = cursor.fetchall()
    for row in result:
    print(row)
    cursor.close()
    conn.close()
    

    And I didn't get any errors. It worked fine and gave me the correct results:

    ('gs://1/mmap',)
    

    So, if you're still stuck, you might want to check if everything is set up correctly on your end, or if there's something different about how your database is expecting the data.