Python Forum
Grabbing comma separed values from SQLite and putting them in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grabbing comma separed values from SQLite and putting them in a list
#9
Here are 2 examples for mock use case
using separate tables:
import random
import sqlite3

def measurments(num):
    sensors = ('Sensor1', 'Sensor2', 'Sensor3')
    for _ in range(num):
        yield (random.choice(sensors), random.randint(30, 100))


class Sensor:
    def __init__(self, sensor_id, sensor_type, sensor_brand, values=None):
        self.id = sensor_id
        self.type = sensor_type
        self.brand = sensor_brand
        self.values = values or []

    def add_value(self, value):
        self.values.append(value)

    
    def __str__(self):
        return (f"{self.id}, {self.type}, {self.brand}, values:{self.values}")


def create(db):
    with sqlite3.connect(db) as conn:
        conn.executescript(
"""DROP TABLE IF EXISTS "measurments";
DROP TABLE IF EXISTS "sensors";
CREATE TABLE "measurments" (
	`SensorID`	TEXT NOT NULL,
	`Value`	INTEGER NOT NULL
);
CREATE TABLE "sensors" (
	`SensorID`	TEXT NOT NULL,
	`SensorType`	TEXT NOT NULL,
	`SensorBrand`	TEXT NOT NULL
);
INSERT INTO `sensors` VALUES ('Sensor1','Temperature Sensor','Brand1'),
 ('Sensor2','Humidity Sensor','Brand2'),
 ('Sensor3','Temperature Sensor','Brand2');""")
        conn.commit()


def read_data(db):
    with sqlite3.connect(db) as conn:
        # retrieve all data. This could be defined as view in the db, so that it is simpel in the code
        data = conn.execute("""SELECT sen.SensorID, sen.SensorType, sen.SensorBrand, mes.Value 
                            FROM sensors as sen
                            INNER JOIN measurments as mes
                            ON sen.SensorID = mes.SensorID
                            ORDER BY mes.Rowid""")

        sensors = {}
        for sensor_id, sensor_type, sensor_brand, value in data:
            sensor = sensors.setdefault(sensor_id, Sensor(sensor_id, sensor_type, sensor_brand))
            sensor.add_value(value)
    return sensors


def write_data(db):
    with sqlite3.connect(db) as conn:
        # take measurments and write data to db:
        for measurment in measurments(10): # simulate 10 random observations
            conn.execute("""INSERT INTO measurments VALUES (?, ?);""", measurment)
            conn.commit()


if __name__ == '__main__':
    db = 'sample2.db'
    create(db)
    write_data(db)
    for sensor_id, sensor in read_data(db).items():
        print(sensor)

    # you can do simply
    with sqlite3.connect(db) as conn:
        data = conn.execute("""SELECT value FROM measurments where SensorID='Sensor1';""")
        values = [item for (item, ) in data]
        print(values)
or using one table, with values comma-separated:

import random
import sqlite3

def measurments(num):
    sensors = ('Sensor1', 'Sensor2', 'Sensor3')
    for _ in range(num):
        yield (random.choice(sensors), random.randint(30, 100))

class Sensor:
    def __init__(self, sensor_id, sensor_type, sensor_brand, values=None):
        self.id = sensor_id
        self.type = sensor_type
        self.brand = sensor_brand
        if values is None:
            self.values = []
        else:
            self.values = list(map(int, values.split(',')))

    def add_value(self, value):
        self.values.append(value)

    
    def __str__(self):
        return (f"{self.id}, {self.type}, {self.brand}, values:{self.values}")


# your way, sample1.db

def create(db):
    with sqlite3.connect(db) as conn:
        conn.executescript(
"""DROP TABLE IF EXISTS "sensors";
CREATE TABLE "sensors" (
	`SensorID`	TEXT NOT NULL,
	`SensorType`	TEXT NOT NULL,
	`SensorBrand`	TEXT NOT NULL,
	`Measurments`	TEXT
);
INSERT INTO `sensors` (SensorID,SensorType,SensorBrand, Measurments) VALUES ('Sensor1','Temperature Sensor','Brand1',NULL),
 ('Sensor2','Humidity Sensor','Brand2',NULL),
 ('Sensor3','Temperature Sensor','Brand2',NULL);""")
        conn.commit()

def read_data(db):
    with sqlite3.connect(db) as conn:
    
        # retrieve initial data
        data = conn.execute('SELECT * from sensors;')
        sensors = {}
        for record in data:
            sensor = Sensor(*record)
            sensors[sensor.id] = sensor
    return sensors

def write_data(db):
    with sqlite3.connect(db) as conn:
        # take measurments and write data to db:
        sensors = read_data(db)
        for sensor_id, value in measurments(10): # simulate 10 random observations
            sensor = sensors[sensor_id]
            sensor.add_value(value)
            conn.execute("""UPDATE sensors SET Measurments = ? WHERE `SensorID` = ?;""", (','.join(map(str, sensor.values)), sensor_id))
            conn.commit()


if __name__ == '__main__':
    db = 'sample1.db'
    create(db)
    write_data(db)
    for sensor_id, sensor in read_data(db).items():
        print(sensor)

    with sqlite3.connect(db) as conn:
        data = conn.execute("""SELECT Measurments FROM sensors WHERE SensorID='Sensor1';""")
        values = [int(value) for value in data.fetchone()[0].split(',')]
        print(values)
Note that this is sample implementation, so there might be different ways to do the same.
The setup is - having 3 Sensors, and we collect data from them to write to sqlite database
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Grabbing comma separed values from SQLite and putting them in a list - by buran - Apr-10-2020, 02:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copying the order of another list with identical values gohanhango 7 1,176 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,275 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Comparing List values to get indexes Edward_ 7 1,204 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,720 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,084 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,129 Aug-19-2022, 08:07 PM
Last Post: Winfried
  How to format Excel column with comma? dee 0 1,384 Jun-13-2022, 10:11 PM
Last Post: dee
  Help with subtracting values using SQLite & Python Extra 10 3,440 May-10-2022, 08:36 AM
Last Post: ibreeden
  decimal comma DPaul 9 2,301 Feb-22-2022, 12:25 PM
Last Post: DeaD_EyE
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 15,072 Jan-19-2022, 08:33 AM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020