1

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"], employee["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"?

skaffman
405k96 gold badges825 silver badges775 bronze badges
asked Nov 9, 2010 at 15:56

1 Answer 1

1

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**
Fred Larson
62.3k19 gold badges117 silver badges179 bronze badges
answered Nov 9, 2010 at 16:00
Sign up to request clarification or add additional context in comments.

3 Comments

I do not understand your answer?
Replace posOrg:%s with posOrg=%s
Ah I think its time to take a break from coding. Any suggestions on the original question to general querying in python?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.