A
A
arhipT9992022-04-09 14:31:00
Python
arhipT999, 2022-04-09 14:31:00

How can I make the value of a dictionary be of type list?

import sqlite3

conn = sqlite3.connect('arhipT999.db')
cur = conn.cursor()
id = 0
inf = ['rian_ru']
try:
    info = cur.execute('SELECT * FROM pr WHERE ch_id=?', (id, ))
    if info.fetchone() is None: 
        cur.execute("""CREATE TABLE IF NOT EXISTS pr(
        ch_id INTEGER,
        sp TEXT);
    """)
        conn.commit()

        cur.execute('insert into pr values (?, ?)', (id, inf))
        conn.commit()
except sqlite3.OperationalError:
    cur.execute("""CREATE TABLE IF NOT EXISTS pr(
        ch_id INTEGER,
        sp TEXT);
    """)
    conn.commit()

    cur.execute('insert into pr values (?, ?)', (id, inf))


print(*cur.execute("SELECT * FROM pr;"))
b = dict(cur.execute("SELECT * FROM pr;"))

print(b)
conn.close()

output:
(0, "['rian_ru']")
{0: "['rian_ru']"}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2022-04-09
@arhipT999

Bring the database schema to normal form . List values ​​must be stored in a separate table and linked through a foreign key.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question