-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_connection.py
34 lines (31 loc) · 1.01 KB
/
db_connection.py
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
import mysql.connector
def connect_to_database():
try:
connection = mysql.connector.connect(
host='localhost',
user='username',
password='password',
database='inventory_db'
)
if connection.is_connected():
print('Connected to MySQL database')
return connection
except mysql.connector.Error as e:
print('Error connecting to MySQL:', e)
return None
def update_price(product_id, new_price):
try:
cursor = connection.cursor()
# Update price in the database
cursor.execute('UPDATE products SET price = %s WHERE product_id = %s', (new_price, product_id))
connection.commit()
print('Price updated successfully')
except mysql.connector.Error as e:
print('Error updating price:', e)
finally:
cursor.close()
# Close the connection
def close_connection():
if connection.is_connected():
connection.close()
print('Connection closed')