此代码在conn.commit
行上出错。
Error: ('HY000', "[HY000] [MySQL][ODBC 3.51 Driver]Commands out of sync; you can't run this command now (2014) (SQLEndTran)")
每当我呼叫SP时,ID在表中不断增加,但记录未插入。
@app.route("/insert_user")
def insert_user():
try:
conn = pyodbc.connect("DRIVER={/usr/local/lib/libmyodbc3.so};SERVER=localhost;DATABASE=user_data;USER=user;PASSWORD=user_pass;OPTION=3;autoCommit = True")
cur = conn.cursor()
cur.execute("{call insert_user_sp(0,'aby@gmail.com','345','male','1992-01-12','www.facebook.com','abc','xyz','p','jr','english','i am student')}")
conn.commit()
except Error as e:
print e
finally:
cur.close()
conn.close()
答案 0 :(得分:1)
由于你在Linux上使用MySQL,我建议使用MySQL Python包而不是pyodbc
。我使用pyodbc
连接到Microsoft SQL Server,但在使用MySQL时使用此包:
https://pypi.python.org/pypi/mysqlclient
然后您可以使用cursor.callproc()
:
http://mysqlclient.readthedocs.org/en/latest/user_guide.html?highlight=callproc#cursor-objects
祝你好运!