Friday, 15 April 2022

How to insert values to a Sqlite database from a Python Set or Dict?

I have the following function which produces results;

def get_from_min_match(var):
    temp = []
    count_elem = generate_elem_count()
    for item in count_elem:
        if var <= count_elem[item]:
            temp.append(item)
    return set(temp) if len(set(temp)) > 0 else "None"

def generate_elem_count():
result_data = []
for val in mapper.values():
    if type(val) == list:
        result_data += val
    elif type(val) == dict:
        for key in val:
            result_data.append(key)

count_elem = {elem: result_data.count(elem) for elem in result_data}
return count_elem

I call this function like this;

print(" These meet three values ", get_from_min_match(3))
print(" These meet four values ", get_from_min_match(4))

The output I get from these functions are as follows;

These meet three values {'ULTA', 'CSCO', 'SHW', 'MANH', 'TTWO', 'SAM', 'RHI', 'PAYC', 'AME', 'CCOI', 'RMD', 'AMD', 'UNH', 'AZO', 'APH', 'EW', 'FFIV', 'IEX', 'IDXX', 'ANET', 'SWKS', 'HRL', 'ILMN', 'PGR', 'ATVI', 'CNS', 'EA', 'ORLY', 'TSCO'}
These meet four values {'EW', 'PAYC', 'TTWO', 'AME', 'IEX', 'IDXX', 'ANET', 'RMD', 'SWKS', 'HRL', 'UNH', 'CCOI', 'ORLY', 'APH', 'PGR', 'TSCO'}

Now, I want to insert the output, of the get_from_min_match function into a Sqlite database. Its structure looks like this;

dbase.execute("INSERT OR REPLACE INTO min_match (DATE, SYMBOL, NAME, NUMBEROFMETRICSMET) \
VALUES (?,?,?,?)", (datetime.today(), symbol, name, NUMBEROFMETRICSMET?))
dbase.commit()

So, it's basically the output of the functions inserted into the database. How to achieve this?

date ULTA name 3
date EW name 4
...

should be the result.

How can I achieve this? Thanks!



from How to insert values to a Sqlite database from a Python Set or Dict?

No comments:

Post a Comment