A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-sqlite-select-data-from-table/ below:

Python SQLite - Select Data from Table

Python SQLite - Select Data from Table

Last Updated : 01 Jul, 2025

SELECT statement in SQLite is used to query and retrieve data from one or more tables in a database. It allows you to choose which columns you want to see, filter rows, sort results, and even perform calculations.

Syntax

SELECT * FROM table_name;

Let's discuss it in detail, with examples:

Below is the snapshot of the table named "Geek" we are going to work around in this article:

Creating a Table in SQLite

Let's create a table called GEEK and insert some sample data into it using the Python sqlite3 module.

Python
import sqlite3

# Connecting to sqlite database
connection_obj = sqlite3.connect('geek.db')

# cursor object
cursor_obj = connection_obj.cursor()

connection_obj.execute("""CREATE TABLE GEEK(
  Email varchar(255),
  Name varchar(50),
  Score int
  );""")

connection_obj.execute(
    """INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk1@gmail.com","Geek1",25)""")
connection_obj.execute(
    """INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk2@gmail.com","Geek2",15)""")
connection_obj.execute(
    """INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk3@gmail.com","Geek3",36)""")
connection_obj.execute(
    """INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk4@gmail.com","Geek4",27)""")
connection_obj.execute(
    """INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk5@gmail.com","Geek5",40)""")
connection_obj.execute(
    """INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk6@gmail.com","Geek6",36)""")
connection_obj.execute(
    """INSERT INTO GEEK (Email,Name,Score) VALUES ("geekk7@gmail.com","Geek7",27)""")

connection_obj.commit()

# Close the connection
connection_obj.close()
Read All Rows

To fetch all records from the table, we use the fetchall() method. This will retrieve all the rows.

Syntax:

cursor.fetchall()

Code Example:

Python
import sqlite3

# Connecting to sqlite databse
connection_obj = sqlite3.connect('geek.db')

# cursor object
cursor_obj = connection_obj.cursor()

# to select all column we will use
statement = '''SELECT * FROM GEEK'''

cursor_obj.execute(statement)

print("All the data")
output = cursor_obj.fetchall()
for row in output:
  print(row)

connection_obj.commit()

# Close the connection
connection_obj.close()

Output:

Read Some Rows

Now we will use the Select statement to retrieve data from the table and fetch many records not all. To fetch many records we will use fetchmany() method.

Syntax:

cursor.fetchmany(size)

Code Example:

Python
import sqlite3

# Connecting to sqlite database
connection_obj = sqlite3.connect('geek.db')

# cursor object
cursor_obj = connection_obj.cursor()

# to select all column we will use
statement = '''SELECT * FROM GEEK'''

cursor_obj.execute(statement)

print("Limited data")
output = cursor_obj.fetchmany(5)
for row in output:
  print(row)

connection_obj.commit()

# Close the connection
connection_obj.close()

Output:

Read Only one Row

To retrieve a single row from the result set, we use the fetchone() method. This is useful when you expect only one record to be returned.

Syntax:

cursor.fetchone()

Code Example:

Python
import sqlite3

# Connecting to sqlite database
connection_obj = sqlite3.connect('geek.db')

# cursor object
cursor_obj = connection_obj.cursor()

# to select all column we will use
statement = '''SELECT * FROM GEEK'''

cursor_obj.execute(statement)

print("Only one data")
output = cursor_obj.fetchone()
print(output)

connection_obj.commit()

# Close the connection
connection_obj.close()

Output:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4