使用存储过程插入用户数据会在提交时出错
Inserting user data with stored procedure gives error when committing
此代码在 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 在 table 中不断增加,但没有插入记录。
@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()
由于您在 Linux 上使用 MySQL,我建议您使用 MySQL Python 包而不是 pyodbc
。我使用 pyodbc
连接到 Microsoft SQL 服务器,但是使用 MySQL 时这个包:
https://pypi.python.org/pypi/mysqlclient
那么你可以使用cursor.callproc()
:
http://mysqlclient.readthedocs.org/en/latest/user_guide.html?highlight=callproc#cursor-objects
祝你好运!
此代码在 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 在 table 中不断增加,但没有插入记录。
@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()
由于您在 Linux 上使用 MySQL,我建议您使用 MySQL Python 包而不是 pyodbc
。我使用 pyodbc
连接到 Microsoft SQL 服务器,但是使用 MySQL 时这个包:
https://pypi.python.org/pypi/mysqlclient
那么你可以使用cursor.callproc()
:
http://mysqlclient.readthedocs.org/en/latest/user_guide.html?highlight=callproc#cursor-objects
祝你好运!