2016-04-22 44 views
-1

此代码在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,'[email protected]','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() 
+1

不应该是'cur.commit()'? – MikeTGW

+0

这不是你如何调用存储过程:http://stackoverflow.com/questions/36629878/call-a-mysql-stored-procedure-with-flask-mysqldb/36630050#36630050 – davidism

回答

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

祝你好运!

+0

mysqlclient是否提供了超过MySQL的显着优势连接器/ Python,还是仅仅是个人喜好的问题? (只是好奇,我已经使用了MySQL连接器/ Python几次,并没有抱怨,但我没有做任何事情。) –

+0

它支持Python 2和3,并且积极维护。我推荐它,因为Django在这里:https://docs.djangoproject.com/ja/1.9/ref/databases/#mysql-db-api-drivers – FlipperPA