-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertdata.py
More file actions
52 lines (42 loc) · 1.65 KB
/
insertdata.py
File metadata and controls
52 lines (42 loc) · 1.65 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
import pandas as pd
from sqlalchemy.orm import Session
from config.db import SessionLocal # Use existing database connection
from models.user import User # Import the users table from user.py
from passlib.context import CryptContext
from utils.security import encode_password, hash_password # Import encoding function
# Initialize password hashing
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# CSV File Path
CSV_FILE = "data.csv"
def import_csv_to_db():
"""Reads data from CSV file and inserts it into the MySQL database only if empty."""
db: Session = SessionLocal()
try:
# Check if table is empty
existing_data = db.query(User).first()
if existing_data:
print("Database already has data. Skipping CSV import.")
return
df = pd.read_csv(CSV_FILE)
user_to_insert = []
for _, row in df.iterrows():
hashed_password = hash_password(row['password']) # Hash password
print(f"Hashed password: {hashed_password}")
# encoded_password = encode_password(hashed_password) # Apply encoding
user_to_insert.append(User(
id=int(row['id']),
name=row['name'],
email=row['email'],
password=hashed_password # Store encoded password
))
# Bulk insert all users
db.add_all(user_to_insert)
db.commit()
print("CSV data inserted successfully.")
except Exception as e:
db.rollback()
print(f"Error inserting data: {e}")
finally:
db.close()
# Call function to import data
import_csv_to_db()