0

I am trying to figure out why my mysql insert statement doesn't seem to work with string replacement. Simple example.

#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect(host="localhost", user="***", passwd="***", db="***")
cur = db.cursor()
a = '1'
b = '2'
c = '3'
cur.execute(""" INSERT INTO rented_users (username,login,password) VALUES ('1','2','3')    """)
cur.execute(""" INSERT INTO rented_users (username,login,password) VALUES (%s,%s,%s);""", (a,b,c)

mysql> describe rented_users;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| username | varchar(100) | YES  |     | NULL    |       |
| login    | varchar(100) | YES  |     | NULL    |       |
| password | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

Basically my first insert statement works fine, but when I try to use the second insert statement using string replacement it fails. It doesn't give me a very descriptive error either.

File "insert_test", line 10

                                                                                                    ^
SyntaxError: invalid syntax

What's weird is this exact same syntax seems to work in a different script fine. Really confused. Any assistance would be greatly appreciated.

cheers

1 Answer 1

2
cur.execute(""" INSERT INTO rented_users (username,login,password) VALUES (%s,%s,%s);""", (a,b,c)

Is missing a closing ).

The invalid syntax is probably reported at the next line. This happens because Python syntax lets you add the parentheses on a following line so long as there's only empty space.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Thomas, I knew it was something simple, wonder why it was working in my other script. Bizarre. Nevertheless thankyou.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.