1

Let's say I make a table like so:

db.execute("""
    CREATE TABLE IF NOT EXISTS todos
    (id INTEGER PRIMARY KEY AUTOINCREMENT, 
     job TEXT, 
     duration TEXT, 
     category TEXT, 
     expected_completion TEXT)
    """)

How would I get all of the values for the 'job' column? I'm aware that I could loop through each entry or use the pandas library, but I'm curious to know if there's a standard way to do this via SQLite.

0

1 Answer 1

12

You can use regular SQL to retrieve your data:

import sqlite3

connection = sqlite3.connect("your_sqlite.db")  # connect to your DB
cursor = connection.cursor()  # get a cursor

cursor.execute("SELECT job FROM todos")  # execute a simple SQL select query
jobs = cursor.fetchall()  # get all the results from the above query

Beware that this will return a list of one-element tuples as we're only asking for one element in the SELECT query, if you want it as a raw list you can do it as:

jobs = [job[0] for job in cursor.execute("SELECT job FROM todos")]
Sign up to request clarification or add additional context in comments.

1 Comment

The raw last snippet was exactly what I was looking for. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.