Here’s an example code to read data from an SQLite database using Python and the sqlite3
module
import sqlite3 # connect to the database conn = sqlite3.connect('example.db') # create a cursor object cur = conn.cursor() # execute a SELECT statement cur.execute('SELECT * FROM customers') # fetch all rows from the result set rows = cur.fetchall() # iterate over the rows and print each row for row in rows: print(row) # close the cursor and connection cur.close() conn.close()
In this example, we connect to an SQLite database called example.db
, create a cursor object, and execute a SELECT statement to retrieve all rows from a table called customers
. We then fetch all rows from the result set using the fetchall()
method, iterate over the rows, and print each row to the console. Finally, we close the cursor and connection to the database using the close()
method.