【问题标题】:SQLAlchemy core insert does not insert data into dbSQLAlchemy核心插入不会将数据插入数据库
【发布时间】:2019-05-02 11:45:14
【问题描述】:

我有这样的问题,当我尝试使用connection.execute(table.insert(), list_of_rows) 插入数据时,SQLAlchemy Core 不会插入行。我在没有任何附加参数的情况下构造连接对象,这意味着connection = engine.connect() 和引擎只有一个附加参数engine = create_engine(uri, echo=True)。 除了在 db 中找不到数据外,我在应用程序的日志中也找不到“INSERT”语句。

我在 py.test 测试期间重现的这个问题可能很重要。

我使用的数据库是 docker 容器中的 mssql。

编辑1: 无论我是否使用事务以及是否将插入更改为connection.execute(table.insert().execution_options(autocommit=True), list_of_rows).rowcount

,proxyresult 的行数始终为 -1

编辑2: 我重写了这段代码,现在它可以工作了。我看不出有什么大的不同。

【问题讨论】:

    标签: python sql-server sqlalchemy


    【解决方案1】:

    connection.execute之后插入的行数是多少:

    proxy = connection.execute(table.insert(), list_of_rows) 打印(proxy.rowcount)

    如果rowcount是正整数,则证明它确实将数据写入DB,但可能只存在于事务中,如果是,则可以检查自动提交是否打开:https://docs.sqlalchemy.org/en/latest/core/connections.html#understanding-autocommit

    【讨论】:

    • 行数始终为-1,无论我是否使用事务以及是否将插入更改为connection.execute(table.insert().execution_options(autocommit=True), list_of_rows).rowcount
    猜你喜欢
    • 1970-01-01
    • 2014-03-25
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 2019-04-25
    相关资源
    最近更新 更多