-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_uploader.py
More file actions
38 lines (34 loc) · 1.23 KB
/
sql_uploader.py
File metadata and controls
38 lines (34 loc) · 1.23 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
import mysql.connector
class SQLUploader:
def __init__(self):
self.db_config = {
'host': 'localhost',
'user': 'root',
'password': 'iamrahul', # Add your password if needed
'database': 'health_project'
}
self.conn = None
self.cursor = None
def connect(self):
try:
self.conn = mysql.connector.connect(**self.db_config)
self.cursor = self.conn.cursor()
print("Successfully connected to MySQL.")
return True
except mysql.connector.Error as err:
print(f"MySQL Connection Error: {err}")
return False
def insert_data(self, bpm, spo2):
if self.conn and self.conn.is_connected():
try:
sql = "INSERT INTO pulse_data (bpm, spo2) VALUES (%s, %s)"
self.cursor.execute(sql, (bpm, spo2))
self.conn.commit()
return True
except mysql.connector.Error as err:
print(f"Insert Failed: {err}")
return False
def close(self):
if self.conn and self.conn.is_connected():
self.cursor.close()
self.conn.close()