Chapter 6 - Sqlite & MacOS/Mobile/Browsers

Section 6.1 - Opening Sqlite

Example for opening and exploring Sqlite databases.

Example Usage:

$ python opening_sqlite.py history_db

References:

Opening Sqlite configuration

This function shows an example of opening a Sqlite database with Python. Additional information regarding Sqlite modules can be seen at https://docs.python.org/3/library/sqlite3.html.

def open_sqlite(input_db):
    """Open a SQLite database

    Args:
        input_db: Path to a SQLite database to open

    Returns:
        A connection to a SQLite database
    """
    print("Provided Database: {}".format(input_db))
    return sqlite3.connect(input_db)

Listing Tables configuration

This function shows an example of listing available tables in an opened Sqlite database.

def list_tables(conn):
    """List all tables in a SQLite database

    Args:
        conn: An open connection from a SQLite database

    Returns:
        list: List of table names found in the database
    """
    cur = conn.cursor()
    cur.execute("SELECT name FROM sqlite_master")
    return [i[0] for i in cur.fetchall()]
list_tables(conn)

List all tables in a SQLite database

Parameters

conn – An open connection from a SQLite database

Returns

List of table names found in the database

Return type

list

open_sqlite(input_db)

Open a SQLite database

Parameters

input_db – Path to a SQLite database to open

Returns

A connection to a SQLite database

Indices and tables