Skip to content

Instantly share code, notes, and snippets.

@andrewm4894
Created November 11, 2020 12:03
Show Gist options
  • Select an option

  • Save andrewm4894/da0b945a72cc10199a5f7c715a8b644c to your computer and use it in GitHub Desktop.

Select an option

Save andrewm4894/da0b945a72cc10199a5f7c715a8b644c to your computer and use it in GitHub Desktop.

Revisions

  1. andrewm4894 created this gist Nov 11, 2020.
    52 changes: 52 additions & 0 deletions sqlite_example.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    import sqlite3
    from sqlite3 import Error


    def create_connection(db_file):
    """ create a database connection to the SQLite database
    specified by the db_file
    :param db_file: database file
    :return: Connection object or None
    """
    conn = None
    try:
    conn = sqlite3.connect(db_file)
    print(sqlite3.version)
    except Error as e:
    print(e)

    return conn


    def select_all_tasks(conn):
    """
    Query all rows in the tasks table
    :param conn: the Connection object
    :return:
    """
    cur = conn.cursor()

    query1 = """
    SELECT *
    FROM FACILITIES
    """
    cur.execute(query1)

    rows = cur.fetchall()

    for row in rows:
    print(row)


    def main():
    database = "sqlite_db_pythonsqlite.db"

    # create a database connection
    conn = create_connection(database)
    with conn:
    print("2. Query all tasks")
    select_all_tasks(conn)


    if __name__ == '__main__':
    main()