sqlitebrowser / website

The sqlitebrowser.org website. Created using Hugo & blogdown.
1 stars 7 forks source link

blog/live-databases-are-now-live/ #16

Open utterances-bot opened 8 months ago

utterances-bot commented 8 months ago

Live databases are now .. live! - DB Browser for SQLite

https://sqlitebrowser.org/blog/live-databases-are-now-live/

pranishdon commented 8 months ago

import requests import sqlite3

Step 1: API bata data fetch bhayo

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()

Step 2: Database ko Name jasma connect garney

database_file = "petroleum_data.db"

try:

database ma connect gareyra entity add gardai

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()
justinclift commented 6 months ago

@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: