V
V
Vladimir Korshunov2021-01-28 20:46:55
Python
Vladimir Korshunov, 2021-01-28 20:46:55

How to list all elements of a specific sqlite3(python) database column?

I have a database that has all values ​​entered correctly. I write this code in python:

checkers_subject_list = checkers_db_script.get_checkers_id(subject)
for key in checkers_subject_list:
    print(key)

Description of checkers_db_script module :
import sqlite3

def get_checkers_id(subject):
    conn = sqlite3.connect("checkers_list.db")
    c = conn.cursor()
    c.execute(f"SELECT checker_id FROM checkers WHERE subject = '{subject}'")
    return c.fetchall()

That is, as I understood from the description of the fetchall function , I should return a list of checker_id elements , which in the database are all initially integer. The list is indeed returned, but each element in it looks something like this: (522472272,)
How to fix this and get a list of elements written normally: 522472272? Or maybe it can be done somehow differently, thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2021-01-28
@GashhLab

The author knows that fetchall returns a list, now it's time for the author to know that the elements of this list are tuples. It remains to read the basics of python and learn how to work with this data type.

S
SashaN69, 2021-01-28
@SashaN69

def get_checkers_id(subject):
        conn = sqlite3.connect("checkers_list.db")
        c = conn.cursor()
        с.execute(
            f"SELECT checker_id FROM checkers WHERE subject = '{subject}'")
        rows = с.fetchall()
        l = []
        for row in rows:
            for x in row:
                l.append(x)
        return l

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question