【问题标题】:Inserting user data with stored procedure gives error when committing使用存储过程插入用户数据会在提交时出错
【发布时间】:2016-04-22 03:22:56
【问题描述】:

此代码在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()

【问题讨论】:

  • 不应该是cur.commit()吗?

标签: python mysql flask pyodbc


【解决方案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

祝你好运!

【讨论】:

  • mysqlclient 与 MySQL Connector/Python 相比是否具有显着优势,或者仅仅是个人喜好问题? (只是好奇。我用过 MySQL Connector/Python 几次,没有任何抱怨,但我并没有做任何花哨的事情。)
  • 它支持 Python 2 和 3,并得到积极维护。我推荐它,因为 Django 在这里:docs.djangoproject.com/ja/1.9/ref/databases/…
猜你喜欢
  • 1970-01-01
  • 2013-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多