Tamwyn
Tamwyn

Reputation: 320

Python mysql.connect Integer in SQL Query

I have some python code:

def GetAge(tournaments):         
    cursor = cnxTurnier.cursor()
    for rows in tournaments:
        TurID = rows[0]         
        TurID = int(TurID)       
        query = ("SELECT JahrgID FROM altersklassen WHERE TurnierID = %s")

        cursor.execute(query, TurID)

        altersklassen = cursor.fetchall()

Im using the mysql.connector on python3.4

tournaments is the result of another SQL Query. Then I export the ID of this query and want to get additional informations (1:n relations). MYSQL is telling me that there is an error in my SQL Sytax near to %i. But why TurID is not replacing it? I've got the weird feeling, that the connector re-parses the TurID into something different than MYSQl is expecting. The SELECT is working direct in MYSQL when I replace the %i.

Can anyone tell me what is wrong with my code an wether the TurID = int(TurID) is needed?

I've tried several things over the day and read different documentations, I can't see the issue. Is maybe the whole idea to solve this wrong? I want to have additional informations to another query from another table (1:n relationship).

Upvotes: 1

Views: 2835

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599580

MySQL - or rather, the Python wrapper MySQLdb - is not expecting %i at all. All parameters should be %s.

Upvotes: 2

Related Questions