T
T
TanderOFF2021-08-03 17:21:14
Python
TanderOFF, 2021-08-03 17:21:14

How to pass dictionary to sqlite database?

Good evening everyone.
Have a dictionary:

inv = {"item1":0,
    "item6":0,
    "item8":4}

how to write this dictionary to the database:
await db.execute("""CREATE TABLE IF NOT EXISTS us (
            us TEXT, 
            id INT,
            invent INT,
            check INT
        )""")
       await db.execute("INSERT INTO us  VALUES (?, ?, ?, 0)", (member.name, member.id, inv))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom, 2021-08-06
Maidurov @sylniyduxom

Dictionary in SQL database, as far as I know, you can not shove, but you can shove a line.

import json
# Например так
a = json.dumps({"a": 1})

This function will create a string from the dictionary, and it can already be stored in the database.
To turn a string back into a dictionary, use json.loads([some string])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question