[Python] adding dictionary values to table in SQLITE

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have an sqlite db to hold entries from a series of scd40 co2 sensors running on RPi picos with micropython code. I set up a table autoindexed with these fields: co2, device_id, timestamp, temperature, humidity in that order.

    The sensor sends a dictionary (example:{'co2': 661, 'device_id': 'pico_1', 'timestamp': 1739488255, 'temperature': 21.58999, 'humidity': 30.71489}).

    This is also the dictionary the the server gets (example: {"co2": 639, "device_id": "pico_1", "timestamp": 1739489315, "temperature": 21.21881, "humidity": 30.91478}

    I cant get the data out of the dictionary and into the table. Here is the function I am using.

    def store_reading(reading):
    key_list = (co2, device_id, timestamp, temperature, humidity)
    print(" store_dict is: ", reading)

    print ("Key List is: ", key_list)
    conn = sqlite3.connect('sensor_data.db')
    cursor = conn.cursor()
    for key in key_list:
    cursor.execute('INSERT INTO sensor_readings (key) VALUE(key)')

    conn.commit()
    conn.close()


    I get an error this error: File "/home/pi/Desktop/server.py", line 37, in store_reading cursor.execute('INSERT INTO sensor_readings (key) VALUE(key)')

    I would appreciate any thoughts you may have. I've tried to research this myself. I don't do a lot of db stuff. Thanks for your help

    Continue reading...

Compartilhe esta Página