You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Replit Database中为单个键存储两个值?基于CoinGecko API的加密货币数据存储需求

Question: Can I store multiple values for a single key in Replit Database?

I'm using the CoinGecko API to fetch cryptocurrency data, and I've written a function that pulls JSON data from the API. Right now, I'm storing cryptocurrency IDs as keys and their current prices as values in Replit Database. I want to add a second value to the same key: the 24-hour price change percentage.

Is it possible to set multiple values for a single key in Replit Database? If not, how can I solve this requirement?

Here's my current function code:

def getCryptoPrices(crypto):
    URL = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur'
    r = requests.get(url = URL)
    data = r.json()
    #putting cryptocurrencies with their prices to a database
    for i in range(len(data)):
        db[data[i]['id']] = data[i]['current_price']
    if crypto in db.keys():
        return db[crypto]
    else:
        return None

Answer

Great question! Replit Database is a simple key-value store, which means each key can only map to one direct value—but that value doesn't have to be a single number or string. You can easily package multiple pieces of related data into a composite structure like a Python dictionary, then store that dictionary as the value for your key. This keeps your key (the crypto ID) unique while letting you attach all the relevant data you need.

Here's how to adjust your code to store both the current price and 24h price change:

  1. Modify the database storage step: Instead of storing just the current_price as the value, create a dictionary that includes both the price and the price_change_percentage_24h field from the CoinGecko response.
  2. Update the return logic: When querying for a crypto ID, return the full dictionary (or specific values from it, depending on your needs).

Here's the revised code:

def getCryptoPrices(crypto):
    URL = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur'
    r = requests.get(url=URL)
    data = r.json()
    
    # Store both price and 24h change in a dictionary for each crypto ID
    for item in data:
        crypto_id = item['id']
        db[crypto_id] = {
            'current_price': item['current_price'],
            'price_change_percentage_24h': item['price_change_percentage_24h']
        }
    
    if crypto in db:
        # Return the full dictionary with both values
        return db[crypto]
        # If you only need specific values, you could do something like:
        # return (db[crypto]['current_price'], db[crypto]['price_change_percentage_24h'])
    else:
        return None

How this works:

  • Replit Database natively supports storing Python dictionaries (it handles serializing/deserializing them behind the scenes), so you don't need to manually convert to/from JSON.
  • This approach keeps your data organized: each crypto ID maps to all the relevant metrics you want, making it easy to add more fields later (like market cap, volume, etc.) if needed.
  • When you retrieve the value for a key, you'll get a dictionary that you can access just like any other Python dict (e.g., result = getCryptoPrices('bitcoin'); print(result['price_change_percentage_24h'])).

Alternatively, you could use a list to store the values (e.g., [current_price, price_change_percentage_24h]), but a dictionary is more readable because each value has a clear label.

内容的提问来源于stack exchange,提问作者Lukas Juskevicius

火山引擎 最新活动