Open utterances-bot opened 8 months ago
import requests import sqlite3
api_url = "https://raw.githubusercontent.com/younginnovations/internship-challenges/master/programming/petroleum-report/data.json" response = requests.get(api_url)
if response.status_code == 200: data = response.json() else: print("Failed to fetch data from the API.") exit()
database_file = "petroleum_data.db"
try:
conn = sqlite3.connect(database_file)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS petroleum_sales (
id INTEGER PRIMARY KEY,
country TEXT,
product TEXT,
year INTEGER,
sales REAL
)
''')
# Data haldai
for entry in data:
cursor.execute('''
INSERT INTO petroleum_sales (country, product, year, sales)
VALUES (?, ?, ?, ?)
''', (entry['country'], entry['petroleum_product'], entry['year'], entry['sale']))
# Print the inserted data
print("Inserted data:", entry)
conn.commit()
print("Data successfully stored in the SQLite database.")
except sqlite3.Error as e: print("SQLite Error:", e) exit()
finally:
conn.close()
@pranishdon Looking over your code, it's not clear what it's trying to do?
It doesn't seem to be doing anything with live databases, or am I misunderstanding? :smile:
Live databases are now .. live! - DB Browser for SQLite
https://sqlitebrowser.org/blog/live-databases-are-now-live/