Database URL

This module contains a helper function to generate a database connection from aURL connection string.

  • connect(url, **connect_params)
  • Create a Database instance from the given connection URL.

Examples:

  1. import os
  2. from playhouse.db_url import connect
  3.  
  4. # Connect to the database URL defined in the environment, falling
  5. # back to a local Sqlite database if no database URL is specified.
  6. db = connect(os.environ.get('DATABASE') or 'sqlite:///default.db')
  • parse(url)
  • Parse the information in the given URL into a dictionary containingdatabase, host, port, user and/or password. Additionalconnection arguments can be passed in the URL query string.

If you are using a custom database class, you can use the parse()function to extract information from a URL which can then be passed in toyour database object.

  • registerdatabase(_db_class, *names)

Parameters:

  • db_class – A subclass of Database.
  • names – A list of names to use as the scheme in the URL, e.g. ‘sqlite’ or ‘firebird’

Register additional database class under the specified names. This functioncan be used to extend the connect() function to support additionalschemes. Suppose you have a custom database class for Firebird namedFirebirdDatabase.

  1. from playhouse.db_url import connect, register_database
  2.  
  3. register_database(FirebirdDatabase, 'firebird')
  4. db = connect('firebird://my-firebird-db')