Reputation: 243
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
Upvotes: 0
Views: 558
Reputation: 55197
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.
Upvotes: 2