1

Is it possible for me to take data stored in a sqlite3 table and use it as a Python variable? I'm looking for something that might be similar to this pseudo-code:

import sqlite3
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
variable = cursor.execute("fetch data from table")
2
  • 2
    Why can't you just do a SELECT? Commented Mar 30, 2014 at 20:39
  • @CL. I wanted to take a specific column from the table Commented Apr 1, 2014 at 7:00

2 Answers 2

3

To read a single value from a table, use a SELECT query that returns a result with a single row and a single column:

for row in cursor.execute("SELECT MyColumn FROM MyTable WHERE ID = ?", [123]):
    variable = row[0]
    break
else:
    variable = 0  # not found
Sign up to request clarification or add additional context in comments.

Comments

-2

Use something like this:

SELECT COUNT(*) FROM table_name

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.