-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_setup.py
More file actions
43 lines (35 loc) · 1.4 KB
/
db_setup.py
File metadata and controls
43 lines (35 loc) · 1.4 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
import pandas as pd
from sqlalchemy import create_engine
import os
def setup_database():
try:
# Check if book_data.csv exists
if not os.path.exists('book_data.csv'):
print("Error: book_data.csv not found!")
return False
# Read sample data
print("Reading book data...")
df = pd.read_csv('book_data.csv')
print(f"Found {len(df)} books in the dataset.")
# Create SQLite database and save data
print("Creating database...")
engine = create_engine('sqlite:///books.db')
df.to_sql('books', engine, if_exists='replace', index=False)
# Verify the data was inserted
verification_df = pd.read_sql('SELECT COUNT(*) as count FROM books', engine)
count = verification_df['count'].iloc[0]
if count == len(df):
print(f"Success! Database created with {count} books.")
return True
else:
print(f"Warning: Expected {len(df)} books but found {count} in database.")
return False
except Exception as e:
print(f"Error setting up database: {str(e)}")
return False
if __name__ == "__main__":
success = setup_database()
if success:
print("Database setup completed successfully!")
else:
print("Database setup failed. Please check the error messages above.")