Once connected to SQLite, you can execute SQL queries to retrieve data from your SQLite database.
cursor object using the cursor() connection method.
my_cursor = my_connection.cursor()
execute() cursor method.
my_cursor.execute("SELECT * FROM employees")
fetch*() methods.
for row in my_cursor.fetchall():
print(row)
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 theexecute() 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.