问题
I am trying to delete a record in database using MySQLdb module. In https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html, I found multi=True
for executing multiple queries in execution but it generates error. Can someone help me knowing that what I am missing?
query = "DELETE FROM Service_Machine WHERE Id=(SELECT Id FROM Machines WHERE Id="+id+");" \
"DELETE FROM Machine_Usage WHERE Id=(SELECT Id FROM Machines WHERE Id="+id+");" \
"DELETE FROM Machines WHERE Id="+id+");
print(query)
self.cursor.execute(query, multi=True)
回答1:
MySQLdb and mysql-connector are two separate, independent MySQL database adapters for Python.
Both of these adapters conform to the the DB API specification versions 2.0.
The multi
keyword parameter is not part of the standard DB API, and is not part of MySQLdb
's API.
Only the mysql-connector
's execute method has the multi
keyword parameter.
回答2:
Try this:
InSql1 = "DELETE FROM Service_Machine WHERE Id=(SELECT Id FROM Machines WHERE Id="+id+");"
InSql2 = "DELETE FROM Machine_Usage WHERE Id=(SELECT Id FROM Machines WHERE Id="+id+");"
InSql3 = "DELETE FROM Machines WHERE Id="+id+");
#print(query)
self.cursor.execute(InSql1)
self.cursor.execute(InSql2)
self.cursor.execute(InSql3)
self.cursor.execute("Commit;")
回答3:
Use the following method instead
try:
cur.execute(query1)
cur.execute(query2)
cur.execute(query150)
con.commit()
except Exception as e:
con.rollback()
来源:https://stackoverflow.com/questions/35322523/why-unexpected-keyword-multi-in-pythons-mysqldb-module