MySQL Update报告错误,我缺少什么?

时间:2010-11-04 15:48:04

标签: python mysql

我注意到有关使用python的MySQL更新的一些帖子,但似乎仍然无法正确使用。

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"

在第25行(updateEmployee)上显示的结果错误,该引用在上面的行前面有(25)。

[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")

看来我最终会在'WHERE子句中加上',但我不明白这是怎么回事?

我失败时使用的员工对象定义为:

{
    'employeeID': '1111', 
    'firstName': 'Linda', 
    'title': 'Systems Manager', 
    'nameN': 'adam.804', 
    'lastName': 'Adam', 
    'locCode': 'TNC', 
    'phone': '555-555-5555', 
    'parentID': '2222', 
    'room': ''
}

1 个答案:

答案 0 :(得分:1)

我认为你的WHERE子句之前有一个额外的逗号。

更新应如下所示:

update foo
set a = 1,
b = 2
where type = 0

我认为你的看起来像这样:

update foo
set a = 1,
b = 2,
where type = 0