Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»python»Performing Transactions
    python

    Performing Transactions

    March 30, 2023No Comments2 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Managing Financial Transactions

    Transactions guarantee that the database will always contain consistent data. Throughout the execution of the database operations, we have to take precautions to ensure that no more than one application modifies any of the entries. The following is included in the transactions:

    properties.


    1. Atomicity


      Either the transaction completes, or nothing happens. If a transaction contains 4 queries then all these queries must be executed, or none of them must be executed.

    2. Consistency


      The database must be consistent before the transaction starts and the database must also be consistent after the transaction is completed.

    3. Isolation


      Intermediate results of a transaction are not visible outside the
      current transaction.

    4. Durability


      Once a transaction was committed, the effects are persistent,
      even after a system failure.

    Python commit() method

    Python has a function called commit(), which guarantees that any modifications made will be saved.

    the database be maintained in a consistent manner.

    It is provided here with the syntax to utilise the commit() function.

    below.



    1. conn.commit()


      #conn is the connection object



    Until the commit() function is called, none of the activities that would change the database’s entries would be carried out.

    called.

    Python rollback() method

    It is possible to undo the modifications that have been made to the database by using the rollback() function. We are able to rollback that transaction in the event that an issue happens when we are working with the database, which allows us to keep the database consistent. This approach is helpful because of this feature.

    The syntax for using the rollback() function is shown below.

    below.



    1. Conn.rollback()

    Closing the connection

    After we have finished all of the actions that pertain to the database, we will need to terminate our connection to the database. Python has a function called close() that you may use. The syntax that must be used in order to call the close() function is shown below.

    below.



    1. conn.close()

    In the following example, we are eliminating all of the staff members that are currently employed by the CS.

    department.

    Example



    1. import


      mysql.connector




    2. #Create the connection object




    3. myconn = mysql.connector.connect(host =

      “localhost”


      , user =


      “root”


      ,passwd =


      “google”


      ,database =


      “PythonDB”


      )




    4. #creating the cursor object




    5. cur = myconn.cursor()



    6. try


      :


    7. cur.execute(

      “delete from Employee where Dept_id = 201”


      )


    8. myconn.commit()


    9. print


      (


      “Deleted !”


      )



    10. except


      :



    11. print


      (


      “Can’t delete !”


      )


    12. myconn.rollback()


    13. myconn.close()

    Output:

    Deleted !
    
    Learn Python free Performing Transactions Python Code Python Course Free download python coursefree Courses Download Python Language
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticlePython List extend
    Next Article Python Pyfiglet Module

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.