Python Forum
Rows not adding to sqlite3 database using SQLAlchemy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rows not adding to sqlite3 database using SQLAlchemy
#12
(Jun-01-2023, 03:45 PM)Calab Wrote: When I get a successful connection, it will return from the function, so never do the finally block. The db shouldn't close.

No, that's incorrect. The finally block always runs, even if you return out of the try. That's the point of the finally block.

def myfunc():
    try:
        a = 25
        return a
    except Exception as e:
        print (f"Got a problem.  {e}")
    finally:
        print ("Running finally.  Closing DB")

answer = myfunc()
print(f"I got {answer} from the function")
Output:
Running finally. Closing DB I got 25 from the function
Why not just put the close in the exception block? What do you expect to happen in finally that wouldn't happen in an exception?
Calab likes this post
Reply


Messages In This Thread
RE: Rows not adding to sqlite3 database using SQLAlchemy - by bowlofred - Jun-02-2023, 05:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create SQLite3 database with peewee Jim53_1980 2 720 Dec-20-2023, 02:38 PM
Last Post: buran
  Regex replace in SQLite3 database WJSwan 1 836 Dec-04-2023, 05:55 PM
Last Post: Larz60+
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 795 Aug-09-2023, 05:51 PM
Last Post: Calab
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,430 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,652 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Python Variables and Sqlite3 Database Staples200 1 3,214 May-25-2021, 02:40 AM
Last Post: Staples200
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,149 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,254 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  sqlite3 database problem Maryan 2 2,530 Oct-05-2020, 05:21 AM
Last Post: buran
  Adding data to a table in SQLite3 djwilson0495 2 3,103 Aug-15-2020, 02:48 PM
Last Post: djwilson0495

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020