From 36f1c2c18dd120ebae3eba26e243c437e7156583 Mon Sep 17 00:00:00 2001 From: Shashank Vivek Date: Mon, 11 Jul 2022 00:08:32 +0530 Subject: [PATCH 1/4] Removing Upsource commit text Removing Upsource commit text --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 2ef9c59..c7a2347 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1 @@ MySQL-python -checking upsource. From a9ebf2d1d7ba5416eef85e0d05e8db65cc2a5d2b Mon Sep 17 00:00:00 2001 From: Shashank Vivek Date: Mon, 11 Jul 2022 00:58:27 +0530 Subject: [PATCH 2/4] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 13c06d0..ea6dd22 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ Note : It's under development. +Blog Link : https://shashankvivek.in/2022/07/11/using-python-to-execute-mysql-insert-and-update-queries/ + This is a MySQL command line utility project written in Python. You can do below things. From c5d558b859d2a9b2018970d4d230616c5d6a8521 Mon Sep 17 00:00:00 2001 From: Shashank Vivek Date: Mon, 11 Jul 2022 00:58:40 +0530 Subject: [PATCH 3/4] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index ea6dd22..fa716d7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # mysql-python-util -Note : It's under development. - Blog Link : https://shashankvivek.in/2022/07/11/using-python-to-execute-mysql-insert-and-update-queries/ This is a MySQL command line utility project written in Python. From 999a533aebbff57b14d81bce6a1f1012254cc4bc Mon Sep 17 00:00:00 2001 From: Shashank Vivek Date: Sat, 1 Nov 2025 19:36:54 +0530 Subject: [PATCH 4/4] Updated insert_update.py with improved code --- insert_update.py | 68 ++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/insert_update.py b/insert_update.py index cb71d10..125d0f3 100644 --- a/insert_update.py +++ b/insert_update.py @@ -3,41 +3,57 @@ import argparse from mysql_config import DB_MYSQL -db = MySQLdb.connect(host=DB_MYSQL['host'], - user=DB_MYSQL['user'], - passwd=DB_MYSQL['passwd'], - db=DB_MYSQL['database']) +def get_db_connection(): + """ + Establish and return a new MySQL database connection. + """ + return MySQLdb.connect( + host=DB_MYSQL['host'], + user=DB_MYSQL['user'], + passwd=DB_MYSQL['passwd'], + db=DB_MYSQL['database'] + ) +def execute_update_query(query: str) -> bool: + """ + Executes an INSERT or UPDATE query with manual commit confirmation. -def execute_update_query(query): + Args: + query (str): The SQL query to execute. + + Returns: + bool: True if committed, False if rolled back. + """ + db = get_db_connection() try: - cur = db.cursor() - cur.execute(query) - nums_of_rows_effected = cur.rowcount - print "Total number of rows to be commited is : %s" % nums_of_rows_effected - user_input = raw_input("Proceed with commiting (y/n) : ") - if user_input.lower() == 'y': - db.commit() - print("Commited !!") - elif user_input.lower() == 'n': - db.rollback() - print("Rolled Back") - else: - print("Invalid Option. \n Valid Options are y and n") - db.rollback() - sys.exit(1) - except KeyboardInterrupt as ke: + with db.cursor() as cur: + cur.execute(query) + nums_of_rows_effected = cur.rowcount + print("Total number of rows to be committed is: %s" % nums_of_rows_effected) + user_input = raw_input("Proceed with committing (y/n): ") + if user_input.lower() == 'y': + db.commit() + print("Committed!") + return True + elif user_input.lower() == 'n': + db.rollback() + print("Rolled Back") + return False + else: + print("Invalid Option. Valid Options are y and n") + db.rollback() + sys.exit(1) + except KeyboardInterrupt: db.rollback() + print("Interrupted. Rolled Back") sys.exit(1) except Exception as e: - print e - + db.rollback() + print("Error:", e) + sys.exit(1) finally: db.close() - return True - - if __name__ == '__main__': parser = argparse.ArgumentParser(description='Insert / Update MySQL queries from here.') parser.add_argument('--query', required=True, type=str, help='MySQL query to execute')