After starting with python last week after coming from php I am wondering what is the best way to build a MySQL query.
I am using Python's MySQLdb module.
After doing some research it seems the best way is something similar to the following code below:
def updateEmployee(employee):
try:
cursor = db.cursor()
cursor.execute("""
UPDATE `employee2`
SET `employeeID`=%s, `parentNameN`=%s, `firstName`=%s, `lastName`=%s, `title`=%s, `room`=%s, `locCode`=%s, `posOrg`=%s
WHERE `employee2`.`nameN`=%s
""", (employee["employeeID"], employe开发者_JAVA技巧e["parentNameN"], employee["firstName"], employee["lastName"], employee["title"], employee["room"], employee["locCode"], employee["posOrg"], employee["nameN"]))
db.commit()
print employee["nameN"]+" updated"
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
This is all great except, when this errors I get something such as "Error 1064: You have an error in your SQL syntax:..." which is nice, it says my query was syntactically incorrect.
So I got that part, but how can I view the query in question? Is there a way I can construct this query outside of the cursor.execute so that in situations where I get generic error messages I can at least do a "print query"?
You don't need backquotes in your query. Mistake is here `posOrg`:%s
SET `employeeID`=%s, `parentNameN`=%s, `firstName`=%s, `lastName`=%s,
`title`=%s, `room`=%s, `locCode`=%s, **`posOrg`:%s**
精彩评论