Skip to content

Latest commit

 

History

History
23 lines (14 loc) · 883 Bytes

connecting-to-and-querying-sqlite.md

File metadata and controls

23 lines (14 loc) · 883 Bytes

Connecting to and querying SQLite database

Connecting to and querying SQLite database is extermely easy with Python. All we need is an SQLite3 module. The below examples shows how to connect to the database, perform a query, and use cursor to fetch the results.

One thing that is weird to me is why we need a cursor at all. As this StackOverflow answer explains, the cursor is a type of abstraction used in databases that enables traversal over the records in a database, more about it here

import sqlite3

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

cursor = conn.cursor()
query = "select * from <table>;"
cursor.execute(query)

first_result = cursor.fetchone()

next_five_results = cursor.fetchmany(5)

all_the_rest = cursor.fetchall()

conn.close()