MySQL Update reports error, What am I missing?
I noticed a few posts on SO regarding MySQL updates using python and still cannot seem to get this correct.
def updateEmployee(employee):
print employee["firstName"]+" "+employee["lastName"]+" "+employee["nameN"]
cursor = db.cursor()
if(len(employee["nameN"]) > 0):
cursor.execute("""
UPDATE `employee`
SET `employeeID`=%s, `parentID`=%s, `firstName`=%s, `title`=%s, `locCode`=%s,
(25)
WHERE `employee`.`nameN`=%s
""", (employee["employeeID"], employee["parentID"], employee["firstName"], employee["title"], employee["locCode"], employee["nameN"]))
if(cursor.rowcount > 0 ):
print "updated"
else:
c.execute("""
INSERT INTO `employee`
(`employeeID`, `parentID`, `nameN`, `firstName`, `alias`, `lastName`, `title`, `department`, `phone`, `areaMission`, `leadershipStyle`, `employeeType`, `coreFunc1`, `coreFunc2`, `coreFunc3`, `address`, `locCode`,`posOrg`)
VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""", (employee["employeeID"], employee["parentID"], employee["nameN"], employee["firstName"], "", employee["lastName"], employee["title"], "", employee["phone"], '', '', '', '', '', '', '', employee["locCode"], ''))
if(c.rowcount > 0):
db.commit()
print "inserted"
开发者_开发技巧 else:
print "insert failed"
else:
print "missing nameN"
Resulting Error which appaers on line 25 (updateEmployee) which for reference has (25) in front of the line above.
[chris@apps ~]$ ./adjust.py
Linda Adam adam.804
Traceback (most recent call last):
File "./adjust.py", line 89, in <module>
processCSV(fileName)
File "./adjust.py", line 14, in processCSV
updateEmployee(employee)
File "./adjust.py", line 25, in updateEmployee
WHERE `employee`.`nameN`=%s """, (employee["employeeID"], employee["parentID"], employee["firstName"], employee["title"], employee["locCode"], employee["nameN"]))
File "/usr/lib/python2.5/site-packages/MySQLdb/cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.5/site-packages/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `employee`.`nameN`='adam.804'' at line 3")
It appears I am ending up with an extra ' at the "WHERE clause" but I do not understand how this could be?
The employee object I am using when this fails is defined as:
{
'employeeID': '1111',
'firstName': 'Linda',
'title': 'Systems Manager',
'nameN': 'adam.804',
'lastName': 'Adam',
'locCode': 'TNC',
'phone': '555-555-5555',
'parentID': '2222',
'room': ''
}
I think you have an extra comma before your WHERE clause.
An update should look like this:
update foo
set a = 1,
b = 2
where type = 0
I think yours looks like this:
update foo
set a = 1,
b = 2,
where type = 0
精彩评论