Inserting Variables MySQL Using Python, Not Working

后端 未结 1 1217

I want to insert the variable bob, and dummyVar into my table, logger. Now from what I can tell all I should need to do is, well what

相关标签:
1条回答
  • 2020-12-10 10:31

    You need to pass the SQL statement and the parameters as separate arguments:

    cursor.execute(loggit[0], loggit[1])
    

    or use the variable argument syntax (a splat, *):

    cursor.execute(*loggit)
    

    Your version tries to pass in a tuple containing the SQL statement and bind parameters as the only argument, where the .execute() function expects to find just the SQL statement string.

    It's more usual to keep the two separate and perhaps store just the SQL statement in a variable:

    loggit = """
            INSERT INTO logger (logged_info, dummy)
            VALUES
                (%s, %s)
        """
    cursor.execute(loggit, (bob, dummyVar))
    
    0 讨论(0)
提交回复
热议问题