-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_update.py
More file actions
61 lines (56 loc) · 1.81 KB
/
insert_update.py
File metadata and controls
61 lines (56 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import MySQLdb
import sys
import argparse
from mysql_config import DB_MYSQL
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.
Args:
query (str): The SQL query to execute.
Returns:
bool: True if committed, False if rolled back.
"""
db = get_db_connection()
try:
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:
db.rollback()
print("Error:", e)
sys.exit(1)
finally:
db.close()
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')
args = parser.parse_args()
execute_update_query(args.query)