diff --git a/README.md b/README.md index e64d102..102b5eb 100644 --- a/README.md +++ b/README.md @@ -1456,14 +1456,13 @@ pprint(, width=80, depth=None) Input ----- -* **Reads a line from user input or pipe if present.** -* **Trailing newline gets stripped.** -* **Prompt string is printed to the standard output before reading input.** -* **Raises EOFError when user hits EOF or input stream gets exhausted.** - +**Reads a line from user input or pipe if present.** ```python = input(prompt=None) ``` +* **Trailing newline gets stripped.** +* **Prompt string is printed to the standard output before reading input.** +* **Raises EOFError when user hits EOF or input stream gets exhausted.** Command Line Arguments @@ -1795,6 +1794,8 @@ def write_to_csv_file(filename, rows): SQLite ------ **Server-less database engine that stores each database into separate file.** + +### Connect ```python import sqlite3 db = sqlite3.connect('') # Also ':memory:'. @@ -1804,12 +1805,12 @@ db.close() * **New database will be created if path doesn't exist.** ### Read +**Returned values can be of type str, int, float, bytes or None.** ```python = db.execute('') # Can raise sqlite3.OperationalError. = .fetchone() # Returns next row. Also next(). = .fetchall() # Returns remaining rows. ``` -* **Returned values can be of type str, int, float, bytes or None.** ### Write ```python @@ -1824,23 +1825,23 @@ with db: ``` ### Placeholders +* **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.** +* **Bools will be stored and returned as ints and dates as [ISO formatted strings](#encode).** ```python db.execute('', ) # Replaces '?'s in query with values. db.execute('', ) # Replaces ':'s with values. db.executemany('', ) # Runs execute() many times. ``` -* **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.** -* **Bools will be stored and returned as ints and dates as [ISO formatted strings](#encode).** ### Example +**In this example values are not actually saved because `'db.commit()'` is omitted!** ```python >>> db = sqlite3.connect('test.db') >>> db.execute('create table t (a, b, c)') >>> db.execute('insert into t values (1, 2, 3)') >>> db.execute('select * from t').fetchall() [(1, 2, 3)] -``` -* **In this example values are not actually saved because `'db.commit()'` was omitted.** +``` ### MySQL **Has a very similar interface, with differences listed below.** diff --git a/index.html b/index.html index df73983..94d9c36 100644 --- a/index.html +++ b/index.html @@ -1351,15 +1351,15 @@ pprint(<collection>, width=80, depth=
  • Levels deeper than 'depth' get replaced by '…'.
  • -

    #Input

      -
    • Reads a line from user input or pipe if present.
    • -
    • Trailing newline gets stripped.
    • -
    • Prompt string is printed to the standard output before reading input.
    • -
    • Raises EOFError when user hits EOF or input stream gets exhausted.
    • -
    <str> = input(prompt=None)
    +

    #Input

    Reads a line from user input or pipe if present.

    <str> = input(prompt=None)
     
    +
      +
    • Trailing newline gets stripped.
    • +
    • Prompt string is printed to the standard output before reading input.
    • +
    • Raises EOFError when user hits EOF or input stream gets exhausted.
    • +

    #Command Line Arguments

    import sys
     script_name = sys.argv[0]
     arguments   = sys.argv[1:]
    @@ -1603,24 +1603,23 @@ shutil.copytree(from, to)          # Copies the entir
             writer.writerows(rows)
     
    -

    #SQLite

    Server-less database engine that stores each database into separate file.

    import sqlite3
    +

    #SQLite

    Server-less database engine that stores each database into separate file.

    Connect

    import sqlite3
     db = sqlite3.connect('<path>')                  # Also ':memory:'.
     ...
     db.close()
    -
    +
    +
    • New database will be created if path doesn't exist.
    -

    Read

    <cursor> = db.execute('<query>')                # Can raise sqlite3.OperationalError.
    +

    Read

    Returned values can be of type str, int, float, bytes or None.

    <cursor> = db.execute('<query>')                # Can raise sqlite3.OperationalError.
     <tuple>  = <cursor>.fetchone()                  # Returns next row. Also next(<cursor>).
     <list>   = <cursor>.fetchall()                  # Returns remaining rows.
     
    -
      -
    • Returned values can be of type str, int, float, bytes or None.
    • -
    +

    Write

    db.execute('<query>')
     db.commit()
     
    @@ -1629,25 +1628,23 @@ db.commit() db.execute('<query>')
    -

    Placeholders

    db.execute('<query>', <list/tuple>)             # Replaces '?'s in query with values.
    +

    Placeholders

      +
    • Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.
    • +
    • Bools will be stored and returned as ints and dates as ISO formatted strings.
    • +
    db.execute('<query>', <list/tuple>)             # Replaces '?'s in query with values.
     db.execute('<query>', <dict/namedtuple>)        # Replaces ':<key>'s with values.
     db.executemany('<query>', <coll_of_above>)      # Runs execute() many times.
     
    -
      -
    • Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.
    • -
    • Bools will be stored and returned as ints and dates as ISO formatted strings.
    • -
    -

    Example

    >>> db = sqlite3.connect('test.db')
    +
    +

    Example

    In this example values are not actually saved because 'db.commit()' is omitted!

    >>> db = sqlite3.connect('test.db')
     >>> db.execute('create table t (a, b, c)')
     >>> db.execute('insert into t values (1, 2, 3)')
     >>> db.execute('select * from t').fetchall()
     [(1, 2, 3)]
     
    -
      -
    • In this example values are not actually saved because 'db.commit()' was omitted.
    • -
    +

    MySQL

    Has a very similar interface, with differences listed below.

    # $ pip3 install mysql-connector
     from mysql import connector
     db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>)