0

I'm working on a function in python that reads values from MySql. When I insert using the same format there are no errors but when I select it gives me the error mysql.connector.errors.ProgrammingError: Could not process parameters: str(555), it must be of type list, tuple or dict Here is my code that is giving the error

cursor.execute('SELECT * FROM LIBS')
            result = cursor.fetchall()
            for row in result:
                user_id = row[0]
                song_id = row[1]
                if(user_id == userno):
                    cursor.execute("SELECT s_title FROM SONG WHERE s_id = (%s)", (song_id))
                    print(row[1])

What I don't understand is when I use the exact same format for INSERT with (%s) there are no errors

I tried adding a comma inside the song_id and then running them as cursor.execute("SELECT s_title FROM SONG WHERE s_id = (%s)", (song_id,)) but I got the error mysql.connector.errors.InternalError: Unread result found.

Andrew
  • 1
  • 1

1 Answers1

0

params must be a list, tuple or dict (see docs)

cursor.execute("SELECT s_title FROM SONG WHERE s_id = (%s)", (song_id,))

Note the comma after song_id. Without the comma, it's just a str and not a tuple. This is only necessary for creating tuples with one element.

Plagon
  • 2,689
  • 1
  • 11
  • 23
  • Doing it like that gives me error unread result found – Andrew Dec 10 '22 at 17:15
  • Yeah, that's because you are not using the cursor properly. You are executing a statement, but do not consume the result before executing another statement. You can set `buffered=True` when creating the cursor. – Plagon Dec 10 '22 at 17:36