Python Connector for SQLite

Querying data - Python Connector for SQLite

Querying data

Once connected to SQLite, you can execute SQL queries to retrieve data from your SQLite database.

Execute a query

  1. Create a cursor object using the cursor() connection method.
    my_cursor = my_connection.cursor()
  2. Execute a SQL query using the execute() cursor method.
    my_cursor.execute("SELECT * FROM employees")
  3. Retrieve results using one of the fetch*() methods.
    for row in my_cursor.fetchall():
        print(row)

Parameterized queries

You can use parameterized queries to pass variable values to your SQL statements. This allows you to reuse the same query with different data and helps to prevent SQL injection attacks.

Pass parameters as a list or tuple to the execute() method:

query = "SELECT Id, Name FROM Contact WHERE Name = ? AND Email = ?"
params = ["Jordan Sanders", "[email protected]"]
my_cursor.execute(query, params)

results = my_cursor.fetchall()
for row in results:
    print(row)

Each placeholder ? in the query is replaced with a corresponding value from the parameter list.

© 2022-2025 Devart. All Rights Reserved. Request Support Python Connectors Forum Provide Feedback