9951 explained code solutions for 126 technologies


sqliteHow do I use Python to select data from a SQLite database?


Using Python to select data from a SQLite database is a relatively straightforward process. To do this, you'll need to import the sqlite3 module first.

import sqlite3

Next, you'll need to create a connection to the database by passing the database file name as a parameter to the connect() method.

conn = sqlite3.connect("mydatabase.db")

After that, you can create a cursor object and call its execute() method to execute a SQL query.

cursor = conn.cursor()
cursor.execute("SELECT * FROM customers")

The execute() method will return a list of tuples, where each tuple represents a row in the table. You can then loop through the list of tuples and access each column's value using the index.

rows = cursor.fetchall()
for row in rows:
    print("Name:", row[0])
    print("Age:", row[1])
    print("Address:", row[2])

# Output
Name: John
Age: 30
Address: 123 Main Street

Finally, you'll need to close the connection to the database by calling the close() method.

conn.close()

For more information on using Python to select data from a SQLite database, check out the SQLite3 documentation.

Edit this code on GitHub