Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:47:57+00:00 2026-06-17T11:47:57+00:00

I need to insert/update bulk rows via SQLAlchemy. And get inserted rows. I tried

  • 0

I need to insert/update bulk rows via SQLAlchemy. And get inserted rows.

I tried to do it with session.execute:

 >>> posts = db.session.execute(Post.__table__.insert(), [{'title': 'dfghdfg', 'content': 'sdfgsdf', 'topic': topic}]*2)
 >>> posts.fetchall()

 ResourceClosedError                       Traceback (most recent call last)

And with engine:

In [17]: conn = db.engine.connect()  

In [18]: result = conn.execute(Post.__table__.insert(), [{'title': 'title', 'content':  'content', 'topic': topic}]*2)

In [19]: print result.fetchall()
ResourceClosedError: This result object does not return rows. It has been closed automatically.

The same response is an object has been closed automatically. How to prevent it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:47:58+00:00Added an answer on June 17, 2026 at 11:47 am

    First answer – on “preventing automatic closing”.

    SQLAlchemy runs DBAPI execute() or executemany() with insert and do not do any select queries.
    So the exception you’ve got is expected behavior. ResultProxy object returned after insert query executed wraps DB-API cursor that doesn’t allow to do .fetchall() on it. Once .fetchall() fails, ResultProxy returns user the exception your saw.

    The only information you can get after insert/update/delete operation would be number of affected rows or the value of primary key after auto increment (depending on database and database driver).

    If your goal is to receive this kind information, consider checking ResultProxy methods and attributes like:

    • .inserted_primary_key
    • .last_inserted_params()
    • .lastrowid
    • etc

    Second answer – on “how to do bulk insert/update and get resulting rows”.

    There is no way to load inserted rows while doing single insert query using DBAPI. SQLAlchemy SQL Expression API you are using for doing bulk insert/updates also doesn’t provide such functionality.
    SQLAlchemy runs DBAPI executemany() call and relies on driver implementation. See this section of documentation for details.

    Solution would be to design your table in a way that every record would have natural key to identify records (combination of columns’ values that identify record in unique way). So insert/update/select queries would be able to target one record.
    After doing it would be possible to do bulk insert/update first and then doing select query by natual key. Thus you won’t need to know autoincremented primary key value.

    Another option: may be you can use SQLAlchemy Object Relational API for creating objects – then SQLAlchemy may try to optimize insert into doing one query with executemany for you. It worked for me while using Oracle DB.
    There won’t be any optimization for updates out of the box. Check this SO question for efficient bulk update ideas

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Generally when I need to insert/update multiple rows, I will generate (in PHP) a
I need to insert/update data by using link or image I need a code
I have a trigger for insert/update/delete. That is working fine. Also, I need the
I am having issues with ADO.NET 2.0 merging/importing data. I need to update/insert data
I need to perform update/insert simultaneously changing structure of incoming data. Think about Shops
I need to insert, delete and update data using a xml file. I have
I need to write a simple C# .NET application to retrieve, update, and insert
Looking for a combined INSERT/UPDATE/DELETE statement, MERGE is exactly what I need, but I
I have roughly 30M rows to Insert Update in SQL Server per day what
My idea is to create some generic classes for Insert/Update/Select via a C# (3.5)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.