Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 I have below, however this doesn't insert anything into my table at all. If I hard-code what should be written (using 'example' then it writes example to the table, so my connection and syntax for inserting is correct to this point). Any help would be more than appreciated!

conn = mysql.connector.connect(user='username', password='password!',
                              host='Host', database='database')

cursor = conn.cursor()

bob = "THIS IS AN EXAMPLE"
dummyVar = "Variable Test"

loggit = ("""
        INSERT INTO logger (logged_info, dummy)
        VALUES
            (%s, %s)
    """, (bob, dummyVar))

cursor.execute(loggit)
conn.commit()

I have also tried this:

loggit = ("""
        INSERT INTO logger (logged_info, dummy)
        VALUES
            (%(bob)s,(Hello))
    """, (bob))

EDIT: This is working code thanks to Martijn Pieters!

bob = "THIS IS AN EXAMPLE"
dummyVar = "Variable Test"

loggit = "INSERT INTO logger (logged_info, dummy) VALUES (%s, %s)"

cursor.execute(loggit, (bob, dummyVar))
conn.commit()
share|improve this question
 
In addition to your main problem (which Martijn Pieters answers), you've got another one in your last example: (bob) is not a tuple of one element, it's the same thing as bob. You need to write (bob,). –  abarnert May 12 at 11:22
add comment

1 Answer

up vote 2 down vote accepted

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))
share|improve this answer
 
Shouldn't I close the brackets after """ ? –  Steven Byrne May 12 at 11:24
 
@StevenByrne In the last example, no, since there's no opening one and no second value, loggit is a string, not a tuple anymore. –  Joachim Isaksson May 12 at 11:26
 
@StevenByrne: No, there is no opening parens; the """ tripple-quoting string can span multiple lines on it's own just fine. –  Martijn Pieters May 12 at 11:28
 
First, thank you for the help, but I'm still a bit confused, am I suppose to change only the cursor.execute(loggit) to cursor.execute(loggit, (bob, dummyVar)) Or am I suppose to also change the way I create my Insert variable? P.S. Will the same syntax work with a global variable? –  Steven Byrne May 12 at 11:36
 
@StevenByrne: You don't need to change the way you use SQL parameters, just how you call execute(), making sure you pass in 2 arguments. –  Martijn Pieters May 12 at 11:37
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.