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:

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

This code defines a SQLAlchemy schema for a **'coinbase_data'** table, retrieves data from the Coinbase API, creates an object of **'CoinbaseData'** class, and adds it to the SQLAlchemy session. Finally, it commits the transaction to the SQLite database.

Note that this code assumes that you have already installed the required libraries (requests, SQLAlchemy) and created a database file named **'coinbase.db'** in the same directory as your Python script.

user: can you make it so I receive data of every currency or just a list

assistant: Yes, you can modify the code to retrieve data for every currency or a list of currencies by changing the Coinbase API URL to include the currency symbols. Here is an example code that retrieves data for a list of currencies and stores it in the SQLite database:

```python
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**

```python
DATABASE_URI = 'sqlite:///coinbase.db'
```

**Define the SQLAlchemy engine and session**

```python
engine = create_engine(DATABASE_URI)
Session = sessionmaker(bind=engine)
session = Session()
```

**Define the SQLAlchemy Base class**

```python
Base = declarative_base()
```

**Define the schema for the table**

```python
class CoinbaseData(Base):
tablename = 'coinbase_data'
id = Column(Integer, primary_key=True)
timestamp = Column(Integer)
currency = Column(String)
price = Column(Float)
```

**Define the list of currencies to retrieve**

```python
currencies = ['BTC-USD', 'ETH-USD', 'LTC-USD']
```

**Loop through the list of currencies and retrieve data from the Coinbase API**

```Python
for currency in currencies:
url = f'https://api.coinbase.com/v2/prices/{currency}/spot'
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'], currency=currency, price=data['data']['amount'])

# Add the object to the session and commit the transaction
session.add(coinbase_data)
session.commit()
```

This code defines a 'coinbase_data' table schema with three columns: timestamp, currency, and price. It retrieves data for the list of currencies defined in the 'currencies' list, creates a new object of 'CoinbaseData' class for each currency, and stores it in the SQLite database using SQLAlchemy.