2

I am writing and reading from a sql database with tries & excepts. The thought behind the try/except is if for some reason the internet is down or we cannot connect to the server, we will write the sql transactions locally to a text file and then use those statements to update the table. That being said - the try and except only seems to work if there is a connection to the server. We have a table BAR in the DB database on server FOO:

        try:
            conn = pyodbc.connect('DRIVER={SQL Server};SERVER=FOO;DATABASE=DB;UID=user;PWD=password')
            cursor = conn.cursor()
            cursor.execute("UPDATE BAR SET Date = '"+time+"' WHERE ID = "+ID)
            conn.commit()  
        except:
            f = open("vistorlog.txt", "a")
            f.write("UPDATE BAR SET Date = '"+time+"' WHERE ID = "+ID+"\n")
            f.close()

the only instance where this try&except works is when there is an issue with the sql statement i.e. "Update BARS..." fails because there is no table named BARS. If I change the server to FOOS (or in a real life scenario unplug the ethernet cord and leave the table/serve names legitimate) the try and except doesn't work - the program freezes with no error.

Rick
  • 19
  • 4
  • See [this post](https://stackoverflow.com/questions/20142746/what-is-connect-timeout-in-sql-server-connection-string#:~:text=Connect%20Timeout%3D30%20means%2C%20within,connection%20attempt%20to%20waits%20indefinitely.&text=Connection%20Timeout%3D30%20means%20that,seconds%20to%20establish%20a%20connection.) which says to add, e.g., `Connect Timeout=30` to your connection string where `30` is "The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error." –  Apr 21 '21 at 15:58

0 Answers0