nagasurendra commited on
Commit
7e99a33
·
verified ·
1 Parent(s): 74a7c3f

Update utils/database_handler.py

Browse files
Files changed (1) hide show
  1. utils/database_handler.py +11 -3
utils/database_handler.py CHANGED
@@ -1,11 +1,12 @@
1
  import pandas as pd
2
  import os
3
 
4
- # Path to Excel database
5
  BASE_DIR = os.path.abspath(os.path.dirname(__file__))
6
  CUSTOMERS_FILE = os.path.join(BASE_DIR, "../database/biryanihub.xlsx")
7
 
8
- print("Saving data to:", CUSTOMERS_FILE) # Debugging
 
9
 
10
  # Ensure the database folder and file exist
11
  if not os.path.exists(os.path.dirname(CUSTOMERS_FILE)):
@@ -28,6 +29,7 @@ def save_user(name, phone, email, password):
28
  print("Attempting to save user:", name, phone, email, password)
29
 
30
  try:
 
31
  df = pd.read_excel(CUSTOMERS_FILE)
32
  except Exception as e:
33
  print("Error reading Excel file:", e)
@@ -39,6 +41,7 @@ def save_user(name, phone, email, password):
39
  print("Error: Email already exists.")
40
  return False
41
 
 
42
  new_user = {
43
  "Name": str(name).strip(),
44
  "Phone": str(phone).strip(),
@@ -46,22 +49,27 @@ def save_user(name, phone, email, password):
46
  "Password": str(password).strip()
47
  }
48
 
 
49
  df = pd.concat([df, pd.DataFrame([new_user])], ignore_index=True)
50
  df = df[["Name", "Phone", "Email", "Password"]]
51
  df = df.astype(str)
52
 
 
53
  try:
54
  with pd.ExcelWriter(CUSTOMERS_FILE, engine="openpyxl", mode="w") as writer:
55
  df.to_excel(writer, index=False)
56
  print("User saved successfully. Updated data:\n", df)
57
- # Confirm save by reading the file
 
58
  df_check = pd.read_excel(CUSTOMERS_FILE)
59
  print("Data in file after saving:\n", df_check)
 
60
  return True
61
  except Exception as e:
62
  print("Error while saving to Excel:", e)
63
  return False
64
 
 
65
  def debug_read_file():
66
  try:
67
  df = pd.read_excel(CUSTOMERS_FILE)
 
1
  import pandas as pd
2
  import os
3
 
4
+ # Path to the updated Excel file
5
  BASE_DIR = os.path.abspath(os.path.dirname(__file__))
6
  CUSTOMERS_FILE = os.path.join(BASE_DIR, "../database/biryanihub.xlsx")
7
 
8
+ # Debugging: Print the file path to confirm its location
9
+ print("Saving data to:", CUSTOMERS_FILE)
10
 
11
  # Ensure the database folder and file exist
12
  if not os.path.exists(os.path.dirname(CUSTOMERS_FILE)):
 
29
  print("Attempting to save user:", name, phone, email, password)
30
 
31
  try:
32
+ # Read existing data from the file
33
  df = pd.read_excel(CUSTOMERS_FILE)
34
  except Exception as e:
35
  print("Error reading Excel file:", e)
 
41
  print("Error: Email already exists.")
42
  return False
43
 
44
+ # Create new user data
45
  new_user = {
46
  "Name": str(name).strip(),
47
  "Phone": str(phone).strip(),
 
49
  "Password": str(password).strip()
50
  }
51
 
52
+ # Append the new user to the DataFrame
53
  df = pd.concat([df, pd.DataFrame([new_user])], ignore_index=True)
54
  df = df[["Name", "Phone", "Email", "Password"]]
55
  df = df.astype(str)
56
 
57
+ # Save updated data back to the Excel file
58
  try:
59
  with pd.ExcelWriter(CUSTOMERS_FILE, engine="openpyxl", mode="w") as writer:
60
  df.to_excel(writer, index=False)
61
  print("User saved successfully. Updated data:\n", df)
62
+
63
+ # Confirm the data was saved by reading the file again
64
  df_check = pd.read_excel(CUSTOMERS_FILE)
65
  print("Data in file after saving:\n", df_check)
66
+
67
  return True
68
  except Exception as e:
69
  print("Error while saving to Excel:", e)
70
  return False
71
 
72
+ # Debugging: Function to confirm the file's contents
73
  def debug_read_file():
74
  try:
75
  df = pd.read_excel(CUSTOMERS_FILE)