XMMR12 commited on
Commit
62ca1fb
·
verified ·
1 Parent(s): c2e6917

Upload DataWriterJsonToDB.py

Browse files
Files changed (1) hide show
  1. DataWriterJsonToDB.py +80 -0
DataWriterJsonToDB.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List #, Dict, Optional
2
+ import json
3
+ import sqlite3
4
+
5
+ def setup_database():
6
+ """Initialize SQLite database"""
7
+ try:
8
+ conn = sqlite3.connect('plants.db')
9
+ c = conn.cursor()
10
+ c.execute('''CREATE TABLE IF NOT EXISTS plants (
11
+ name TEXT, scientific_name TEXT, alternate_names TEXT,
12
+ description TEXT, plant_family TEXT, origin TEXT, growth_habitat TEXT,
13
+ active_components TEXT, treatable_conditions TEXT, preparation_methods TEXT,
14
+ dosage TEXT, duration TEXT, contraindications TEXT, side_effects TEXT,
15
+ interactions TEXT, part_used TEXT, harvesting_time TEXT, storage_tips TEXT,
16
+ images TEXT, related_videos TEXT, sources TEXT
17
+ )''')
18
+ conn.commit()
19
+ print("Database created successfully!")
20
+ conn.close()
21
+ except sqlite3.Error as e:
22
+ print(f"An error occurred: {e}")
23
+
24
+
25
+ def write_to_database():
26
+ """Save extracted data to SQLite"""
27
+ try:
28
+ conn = sqlite3.connect('plants.db')
29
+ c = conn.cursor()
30
+ with open("plants_data.json", 'r') as f:
31
+ mapped_data = json.load(f)
32
+ # Prepare the insert statement
33
+ insert_sql = '''INSERT INTO plants (name, scientific_name, alternate_names, description, plant_family, origin, growth_habitat, active_components, treatable_conditions, preparation_methods, dosage, duration, contraindications, side_effects, interactions, part_used, harvesting_time, storage_tips, images, related_videos, sources ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'''
34
+ for each in mapped_data:
35
+ c.execute(insert_sql, (
36
+ each.get('Name', ''),
37
+ each.get('Scientific Name', ''),
38
+ each.get('Alternate Names', ''),
39
+ each.get('Description', ''),
40
+ each.get('Plant Family', ''),
41
+ each.get('Origin', ''),
42
+ each.get('Growth Habitat', ''),
43
+ each.get('Active Components', ''),
44
+ each.get('Treatable Conditions', ''),
45
+ each.get('Preparation Methods', ''),
46
+ each.get('Dosage', ''),
47
+ each.get('Duration', ''),
48
+ each.get('Contraindications', ''),
49
+ each.get('Side Effects', ''),
50
+ each.get('Interactions', ''),
51
+ each.get('Part Used', ''),
52
+ each.get('Harvesting Time', ''),
53
+ each.get('Storage Tips', ''),
54
+ each.get('Images', ''),
55
+ each.get('Related Videos', ''),
56
+ each.get('Sources', '')
57
+ ))
58
+ conn.commit()
59
+ conn.close()
60
+ except sqlite3.Error as e:
61
+ print(f"An error occurred: {e}")
62
+
63
+ def view_database()->List:
64
+ conn = sqlite3.connect('plants.db')
65
+ # Create a cursor object
66
+ cursor = conn.cursor()
67
+ # Execute a query to retrieve data
68
+ cursor.execute("SELECT * FROM plants")
69
+ # Fetch all results
70
+ rows = cursor.fetchall()
71
+ # Print the results
72
+ plants=[]
73
+ for row in rows:
74
+ plants.append(row)
75
+ # Close the connection
76
+ conn.close()
77
+ return plants
78
+
79
+ setup_database()
80
+ write_to_database()