write python to store data from coinbase api in sqlite database using sqlalchemy
- User
write python to store data from coinbase api in sqlite database using sqlalchemy
- Assistant
Sure, here is an example code that retrieves data from Coinbase API and stores it in a SQLite database using SQLAlchemy library:
import requests import sqlite3 from sqlalchemy import create_engine, Column, Integer, String, Float from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base # Define the database connection URL DATABASE_URI = 'sqlite:///coinbase.db' # Define the SQLAlchemy engine and session engine = create_engine(DATABASE_URI) Session = sessionmaker(bind=engine) session = Session() # Define the SQLAlchemy Base class Base = declarative_base() # Define the schema for the table class CoinbaseData(Base): __tablename__ = 'coinbase_data' id = Column(Integer, primary_key=True) timestamp = Column(Integer) price = Column(Float) # Define the Coinbase API URL url = 'https://api.coinbase.com/v2/prices/BTC-USD/spot' # Retrieve data from the Coinbase API response = requests.get(url) data = response.json() # Create a new object of CoinbaseData class and store data in it coinbase_data = CoinbaseData(timestamp=data['data']['timestamp'], price=data['data']['amount']) # Add the object to the session and commit the transaction session.add(coinbase_data) session.commit() - User
can you make it so I receive data of every currency or just a list
- Assistant
import requests import sqlite3 from sqlalchemy import create_engine, Column, Integer, String, Float from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base # Define the database connection URL DATABASE_URI = 'sqlite:///coinbase.db' # Define the SQLAlchemy engine and session engine = create_engine(DATABASE_URI) Session = sessionmaker(bind=engine) session = Session() # Define the SQLAlchemy Base class Base = declarative_base() # Define the schema for the table class CoinbaseData(Base): __tablename__ = 'coinbase_data' id = Column(Integer, primary_key=True) currency_pair = Column(String) exchange_rate = Column(Float) # Define the Coinbase API URL url = 'https://api.coinbase.com/v2/exchange-rates' # Retrieve data from the Coinbase API response = requests.get(url) data = response.json() # Loop through the data and create a new object of CoinbaseData class for each currency pair for currency_pair, exchange_rate in data['data'].items(): coinbase_data = CoinbaseData(currency_pair=currency_pair, exchange_rate=float(exchange_rate)) session.add(coinbase_data) # Commit the transactions and close the session session.commit() session.close()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →