Engine 配置¶
Engine Configuration
Engine
是任何SQLAlchemy应用程序的起点。它是实际数据库及其 DBAPI 的“基地”,通过连接池和描述如何与特定类型的数据库/DBAPI组合通信的 Dialect
提供给SQLAlchemy应用程序。
一般结构可以如下图所示:

在上图中,Engine
引用 Dialect
和 Pool
,它们共同解释DBAPI的模块函数以及数据库的行为。
创建引擎只需发出一个简单的调用 create_engine()
:
from sqlalchemy import create_engine
engine = create_engine("postgresql+psycopg2://scott:tiger@localhost:5432/mydatabase")
上述引擎创建了一个针对PostgreSQL定制的 Dialect
对象,以及一个在首次收到连接请求时将在 localhost:5432
建立DBAPI连接的 Pool
对象。注意 Engine
及其底层的 Pool
在调用 Engine.connect()
或 Engine.begin()
方法之前不会建立第一个实际的DBAPI连接。这些方法中的任意一个也可能被其他首次需要数据库连接的SQLAlchemy Engine
依赖对象(如ORM的 Session
对象)调用。通过这种方式,可以说 Engine
和 Pool
具有 延迟初始化 行为。
创建后,可以直接使用 Engine
与数据库交互,或将其传递给 Session
对象以使用ORM。本节介绍了配置 Engine
的详细信息。下一节 使用Engines和Connection 将详细介绍 Engine
及类似对象的使用API,通常用于非ORM应用程序。
The Engine
is the starting point for any SQLAlchemy application. It’s
“home base” for the actual database and its DBAPI, delivered to the SQLAlchemy
application through a connection pool and a Dialect
, which describes how
to talk to a specific kind of database/DBAPI combination.
The general structure can be illustrated as follows:

Where above, an Engine
references both a
Dialect
and a Pool
,
which together interpret the DBAPI’s module functions as well as the behavior
of the database.
Creating an engine is just a matter of issuing a single call,
create_engine()
:
from sqlalchemy import create_engine
engine = create_engine("postgresql+psycopg2://scott:tiger@localhost:5432/mydatabase")
The above engine creates a Dialect
object tailored towards
PostgreSQL, as well as a Pool
object which will establish a
DBAPI connection at localhost:5432
when a connection request is first
received. Note that the Engine
and its underlying
Pool
do not establish the first actual DBAPI connection
until the Engine.connect()
or Engine.begin()
methods are called. Either of these methods may also be invoked by other
SQLAlchemy Engine
dependent objects such as the ORM
Session
object when they first require database connectivity.
In this way, Engine
and Pool
can be said to
have a lazy initialization behavior.
The Engine
, once created, can either be used directly to interact with the database,
or can be passed to a Session
object to work with the ORM. This section
covers the details of configuring an Engine
. The next section, 使用Engines和Connection,
will detail the usage API of the Engine
and similar, typically for non-ORM
applications.
支持的数据库¶
Supported Databases
数据库 URL¶
Database URLs
create_engine()
函数基于 URL 生成一个 Engine
对象。URL 的格式通常遵循 RFC-1738,但也有一些例外,例如在“scheme”部分可以使用下划线,而不能使用短划线或句点。URL 通常包含用户名、密码、主机名、数据库名称字段,以及用于附加配置的可选关键字参数。在某些情况下,可以使用文件路径,而在其他情况下,可以使用“数据源名称”替换“主机”和“数据库”部分。数据库 URL 的典型格式如下:
dialect+driver://username:password@host:port/database
方言名称包括 SQLAlchemy 方言的标识名称,例如“sqlite”、“mysql”、“postgresql”、“oracle”或“mssql”。驱动程序名称是用于连接数据库的 DBAPI 的名称,所有名称均使用小写字母。如果未指定,则会导入“默认”DBAPI(如果可用)——此默认驱动程序通常是该后端最知名的驱动程序。
The create_engine()
function produces an Engine
object based on a URL. The format of the URL generally follows RFC-1738, with some exceptions, including that underscores, not dashes or periods, are accepted within the “scheme” portion. URLs typically include username, password, hostname, database name fields, as well as optional keyword arguments for additional configuration. In some cases a file path is accepted, and in others a “data source name” replaces the “host” and “database” portions. The typical form of a database URL is:
dialect+driver://username:password@host:port/database
Dialect names include the identifying name of the SQLAlchemy dialect, a name such as sqlite
, mysql
, postgresql
, oracle
, or mssql
. The drivername is the name of the DBAPI to be used to connect to the database using all lowercase letters. If not specified, a “default” DBAPI will be imported if available - this default is typically the most widely known driver available for that backend.
转义密码中的特殊字符,例如 @ 符号¶
Escaping Special Characters such as @ signs in Passwords
在构造传递给 create_engine()
的完整 URL 字符串时,
用户名和密码中可能包含的特殊字符需要进行 URL 编码,才能被正确解析。
这包括 @ 符号。
下面是一个包含密码 "kx@jj5/g"
的 URL 示例,其中 “at” 符号和斜杠字符分别表示为
%40
和 %2F
:
postgresql+pg8000://dbuser:kx%40jj5%2Fg@pghost10/appdb
上述密码的编码可以使用 urllib.parse 生成:
>>> import urllib.parse
>>> urllib.parse.quote_plus("kx@jj5/g")
‘kx%40jj5%2Fg’
然后可以将该 URL 字符串传递给 create_engine()
:
from sqlalchemy import create_engine
engine = create_engine("postgresql+pg8000://dbuser:kx%40jj5%2Fg@pghost10/appdb")
除了通过转义特殊字符来构建完整的 URL 字符串外,
还可以将一个 URL
对象实例传递给 create_engine()
,
这样可以跳过字符串解析过程,并直接处理未转义的字符串。
有关示例,请参见下一节。
在 1.4 版本发生变更: 已修复对主机名和数据库名称中 @
符号的支持。
由于该修复,密码中的 @
符号现在必须进行转义。
When constructing a fully formed URL string to pass to
create_engine()
, special characters such as those that may
be used in the user and password need to be URL encoded to be parsed correctly..
This includes the @ sign.
Below is an example of a URL that includes the password "kx@jj5/g"
, where the
“at” sign and slash characters are represented as %40
and %2F
,
respectively:
postgresql+pg8000://dbuser:kx%40jj5%2Fg@pghost10/appdb
The encoding for the above password can be generated using urllib.parse:
>>> import urllib.parse
>>> urllib.parse.quote_plus("kx@jj5/g")
‘kx%40jj5%2Fg’
The URL may then be passed as a string to create_engine()
:
from sqlalchemy import create_engine
engine = create_engine("postgresql+pg8000://dbuser:kx%40jj5%2Fg@pghost10/appdb")
As an alternative to escaping special characters in order to create a complete
URL string, the object passed to create_engine()
may instead be an
instance of the URL
object, which bypasses the parsing
phase and can accommodate for unescaped strings directly. See the next
section for an example.
在 1.4 版本发生变更: Support for @
signs in hostnames and database names has been
fixed. As a side effect of this fix, @
signs in passwords must be
escaped.
以编程方式创建 URL¶
Creating URLs Programmatically
传递给 create_engine()
的参数可以是一个 URL
实例,
而不是一个普通字符串,这样就不需要执行字符串解析,也不需要提供已转义的 URL 字符串。
URL
对象是使用 URL.create()
构造方法创建的,
每个字段都单独传入。密码中的特殊字符可以直接传递,无需修改:
from sqlalchemy import URL
url_object = URL.create(
"postgresql+pg8000",
username="dbuser",
password="kx@jj5/g", # 原始(未转义)文本
host="pghost10",
database="appdb",
)
构造好的 URL
对象随后可以直接传递给 create_engine()
,
用于替代字符串参数:
from sqlalchemy import create_engine
engine = create_engine(url_object)
The value passed to create_engine()
may be an instance of
URL
, instead of a plain string, which bypasses the need for string
parsing to be used, and therefore does not need an escaped URL string to be
provided.
The URL
object is created using the URL.create()
constructor method, passing all fields individually. Special characters
such as those within passwords may be passed without any modification:
from sqlalchemy import URL
url_object = URL.create(
"postgresql+pg8000",
username="dbuser",
password="kx@jj5/g", # plain (unescaped) text
host="pghost10",
database="appdb",
)
The constructed URL
object may then be passed directly to
create_engine()
in place of a string argument:
from sqlalchemy import create_engine
engine = create_engine(url_object)
后端特定的 URL¶
Backend-Specific URLs
Examples for common connection styles follow below. For a full index of detailed information on all included dialects as well as links to third-party dialects, see Dialects.
PostgreSQL¶
PostgreSQL
The PostgreSQL dialect uses psycopg2 as the default DBAPI. Other PostgreSQL DBAPIs include pg8000 and asyncpg:
# default
engine = create_engine("postgresql://scott:tiger@localhost/mydatabase")
# psycopg2
engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/mydatabase")
# pg8000
engine = create_engine("postgresql+pg8000://scott:tiger@localhost/mydatabase")
More notes on connecting to PostgreSQL at PostgreSQL.
MySQL¶
MySQL
MySQL 方言使用 mysqlclient 作为默认 DBAPI。还有其他可用的 MySQL DBAPI,包括 PyMySQL:
# default
engine = create_engine("mysql://scott:tiger@localhost/foo")
# mysqlclient (a maintained fork of MySQL-Python)
engine = create_engine("mysql+mysqldb://scott:tiger@localhost/foo")
# PyMySQL
engine = create_engine("mysql+pymysql://scott:tiger@localhost/foo")
有关连接到 MySQL 的更多说明,请参阅 MySQL 和 MariaDB.
The MySQL dialect uses mysqlclient as the default DBAPI. There are other MySQL DBAPIs available, including PyMySQL:
# default
engine = create_engine("mysql://scott:tiger@localhost/foo")
# mysqlclient (a maintained fork of MySQL-Python)
engine = create_engine("mysql+mysqldb://scott:tiger@localhost/foo")
# PyMySQL
engine = create_engine("mysql+pymysql://scott:tiger@localhost/foo")
More notes on connecting to MySQL at MySQL 和 MariaDB.
Oracle¶
Oracle
首选的 Oracle 数据库方言使用 python-oracledb 驱动程序作为 DBAPI:
engine = create_engine(
"oracle+oracledb://scott:tiger@127.0.0.1:1521/?service_name=freepdb1"
)
engine = create_engine("oracle+oracledb://scott:tiger@tnsalias")
由于历史原因,Oracle 方言使用过时的 cx_Oracle 驱动程序作为默认 DBAPI:
engine = create_engine("oracle://scott:tiger@127.0.0.1:1521/?service_name=freepdb1")
engine = create_engine("oracle+cx_oracle://scott:tiger@tnsalias")
有关连接到 Oracle 数据库的更多说明,请访问 Oracle.
Microsoft SQL Server¶
Microsoft SQL Server
SQL Server 方言使用 pyodbc 作为默认 DBAPI。pymssql 也可用:
# pyodbc
engine = create_engine("mssql+pyodbc://scott:tiger@mydsn")
# pymssql
engine = create_engine("mssql+pymssql://scott:tiger@hostname:port/dbname")
有关连接到 SQL Server 的更多说明,请参阅 Microsoft SQL Server.
The SQL Server dialect uses pyodbc as the default DBAPI. pymssql is also available:
# pyodbc
engine = create_engine("mssql+pyodbc://scott:tiger@mydsn")
# pymssql
engine = create_engine("mssql+pymssql://scott:tiger@hostname:port/dbname")
More notes on connecting to SQL Server at Microsoft SQL Server.
SQLite¶
SQLite
SQLite 连接基于文件的数据库,默认使用 Python 内置模块 sqlite3
.
由于 SQLite 连接到本地文件,URL 格式略有不同。URL 中的“file”部分是数据库的文件名。对于相对文件路径,需要三个斜杠:
# sqlite://<nohostname>/<path>
# where <path> is relative:
engine = create_engine("sqlite:///foo.db")
对于绝对文件路径,三个斜杠后面是绝对路径:
# Unix/Mac - 4 initial slashes in total
engine = create_engine("sqlite:////absolute/path/to/foo.db")
# Windows
engine = create_engine("sqlite:///C:\\path\\to\\foo.db")
# Windows alternative using raw string
engine = create_engine(r"sqlite:///C:\path\to\foo.db")
要使用 SQLite :memory:
数据库,请指定一个空的 URL:
engine = create_engine("sqlite://")
有关连接到 SQLite 的更多说明 SQLite.
SQLite connects to file-based databases, using the Python built-in module sqlite3
by default.
As SQLite connects to local files, the URL format is slightly different. The “file” portion of the URL is the filename of the database. For a relative file path, this requires three slashes:
# sqlite://<nohostname>/<path>
# where <path> is relative:
engine = create_engine("sqlite:///foo.db")
And for an absolute file path, the three slashes are followed by the absolute path:
# Unix/Mac - 4 initial slashes in total
engine = create_engine("sqlite:////absolute/path/to/foo.db")
# Windows
engine = create_engine("sqlite:///C:\\path\\to\\foo.db")
# Windows alternative using raw string
engine = create_engine(r"sqlite:///C:\path\to\foo.db")
To use a SQLite :memory:
database, specify an empty URL:
engine = create_engine("sqlite://")
More notes on connecting to SQLite at SQLite.
其他¶
Others
引擎创建 API¶
Engine Creation API
Object Name | Description |
---|---|
create_engine(url, **kwargs) |
Create a new |
create_mock_engine(url, executor, **kw) |
Create a “mock” engine used for echoing DDL. |
create_pool_from_url(url, **kwargs) |
Create a pool instance from the given url. |
engine_from_config(configuration[, prefix], **kwargs) |
Create a new Engine instance using a configuration dictionary. |
make_url(name_or_url) |
Given a string, produce a new URL instance. |
Represent the components of a URL used to connect to a database. |
- function sqlalchemy.create_engine(url: str | _url.URL, **kwargs: Any) Engine ¶
Create a new
Engine
instance.The standard calling form is to send the URL as the first positional argument, usually a string that indicates database dialect and connection arguments:
engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/test")
备注
Please review 数据库 URL for general guidelines in composing URL strings. In particular, special characters, such as those often part of passwords, must be URL encoded to be properly parsed.
Additional keyword arguments may then follow it which establish various options on the resulting
Engine
and its underlyingDialect
andPool
constructs:engine = create_engine( "mysql+mysqldb://scott:tiger@hostname/dbname", pool_recycle=3600, echo=True, )
The string form of the URL is
dialect[+driver]://user:password@host/dbname[?key=value..]
, wheredialect
is a database name such asmysql
,oracle
,postgresql
, etc., anddriver
the name of a DBAPI, such aspsycopg2
,pyodbc
,cx_oracle
, etc. Alternatively, the URL can be an instance ofURL
.**kwargs
takes a wide variety of options which are routed towards their appropriate components. Arguments may be specific to theEngine
, the underlyingDialect
, as well as thePool
. Specific dialects also accept keyword arguments that are unique to that dialect. Here, we describe the parameters that are common to mostcreate_engine()
usage.Once established, the newly resulting
Engine
will request a connection from the underlyingPool
onceEngine.connect()
is called, or a method which depends on it such asEngine.execute()
is invoked. ThePool
in turn will establish the first actual DBAPI connection when this request is received. Thecreate_engine()
call itself does not establish any actual DBAPI connections directly.- 参数:
connect_args¶ – a dictionary of options which will be passed directly to the DBAPI’s
connect()
method as additional keyword arguments. See the example at 自定义 DBAPI connect() 参数/连接例程.creator¶ –
a callable which returns a DBAPI connection. This creation function will be passed to the underlying connection pool and will be used to create all new database connections. Usage of this function causes connection parameters specified in the URL argument to be bypassed.
This hook is not as flexible as the newer
DialectEvents.do_connect()
hook which allows complete control over how a connection is made to the database, given the full set of URL arguments and state beforehand.参见
DialectEvents.do_connect()
- event hook that allows full control over DBAPI connection mechanics.echo=False¶ –
if True, the Engine will log all statements as well as a
repr()
of their parameter lists to the default log handler, which defaults tosys.stdout
for output. If set to the string"debug"
, result rows will be printed to the standard output as well. Theecho
attribute ofEngine
can be modified at any time to turn logging on and off; direct control of logging is also available using the standard Pythonlogging
module.参见
配置日志记录 - further detail on how to configure logging.
echo_pool=False¶ –
if True, the connection pool will log informational output such as when connections are invalidated as well as when connections are recycled to the default log handler, which defaults to
sys.stdout
for output. If set to the string"debug"
, the logging will include pool checkouts and checkins. Direct control of logging is also available using the standard Pythonlogging
module.参见
配置日志记录 - further detail on how to configure logging.
empty_in_strategy¶ –
No longer used; SQLAlchemy now uses “empty set” behavior for IN in all cases.
自 1.4 版本弃用: The
create_engine.empty_in_strategy
keyword is deprecated, and no longer has any effect. All IN expressions are now rendered using the “expanding parameter” strategy which renders a set of boundexpressions, or an “empty set” SELECT, at statement executiontime.enable_from_linting¶ –
defaults to True. Will emit a warning if a given SELECT statement is found to have un-linked FROM elements which would cause a cartesian product.
在 1.4 版本加入.
execution_options¶ – Dictionary execution options which will be applied to all connections. See
Connection.execution_options()
future¶ –
Use the 2.0 style
Engine
andConnection
API.As of SQLAlchemy 2.0, this parameter is present for backwards compatibility only and must remain at its default value of
True
.The
create_engine.future
parameter will be deprecated in a subsequent 2.x release and eventually removed.在 1.4 版本加入.
在 2.0 版本发生变更: All
Engine
objects are “future” style engines and there is no longer afuture=False
mode of operation.hide_parameters¶ –
Boolean, when set to True, SQL statement parameters will not be displayed in INFO logging nor will they be formatted into the string representation of
StatementError
objects.参见
配置日志记录 - further detail on how to configure logging.
implicit_returning=True¶ – Legacy parameter that may only be set to True. In SQLAlchemy 2.0, this parameter does nothing. In order to disable “implicit returning” for statements invoked by the ORM, configure this on a per-table basis using the
Table.implicit_returning
parameter.insertmanyvalues_page_size¶ –
number of rows to format into an INSERT statement when the statement uses “insertmanyvalues” mode, which is a paged form of bulk insert that is used for many backends when using executemany execution typically in conjunction with RETURNING. Defaults to 1000, but may also be subject to dialect-specific limiting factors which may override this value on a per-statement basis.
在 2.0 版本加入.
isolation_level¶ –
optional string name of an isolation level which will be set on all new connections unconditionally. Isolation levels are typically some subset of the string names
"SERIALIZABLE"
,"REPEATABLE READ"
,"READ COMMITTED"
,"READ UNCOMMITTED"
and"AUTOCOMMIT"
based on backend.The
create_engine.isolation_level
parameter is in contrast to theConnection.execution_options.isolation_level
execution option, which may be set on an individualConnection
, as well as the same parameter passed toEngine.execution_options()
, where it may be used to create multiple engines with different isolation levels that share a common connection pool and dialect.在 2.0 版本发生变更: The
create_engine.isolation_level
parameter has been generalized to work on all dialects which support the concept of isolation level, and is provided as a more succinct, up front configuration switch in contrast to the execution option which is more of an ad-hoc programmatic option.json_deserializer¶ – for dialects that support the
JSON
datatype, this is a Python callable that will convert a JSON string to a Python object. By default, the Pythonjson.loads
function is used.json_serializer¶ – for dialects that support the
JSON
datatype, this is a Python callable that will render a given object as JSON. By default, the Pythonjson.dumps
function is used.label_length=None¶ –
optional integer value which limits the size of dynamically generated column labels to that many characters. If less than 6, labels are generated as “_(counter)”. If
None
, the value ofdialect.max_identifier_length
, which may be affected via thecreate_engine.max_identifier_length
parameter, is used instead. The value ofcreate_engine.label_length
may not be larger than that ofcreate_engine.max_identfier_length
.logging_name¶ –
String identifier which will be used within the “name” field of logging records generated within the “sqlalchemy.engine” logger. Defaults to a hexstring of the object’s id.
max_identifier_length¶ –
integer; override the max_identifier_length determined by the dialect. if
None
or zero, has no effect. This is the database’s configured maximum number of characters that may be used in a SQL identifier such as a table name, column name, or label name. All dialects determine this value automatically, however in the case of a new database version for which this value has changed but SQLAlchemy’s dialect has not been adjusted, the value may be passed here.max_overflow=10¶ – the number of connections to allow in connection pool “overflow”, that is connections that can be opened above and beyond the pool_size setting, which defaults to five. this is only used with
QueuePool
.module=None¶ – reference to a Python module object (the module itself, not its string name). Specifies an alternate DBAPI module to be used by the engine’s dialect. Each sub-dialect references a specific DBAPI which will be imported before first connect. This parameter causes the import to be bypassed, and the given module to be used instead. Can be used for testing of DBAPIs as well as to inject “mock” DBAPI implementations into the
Engine
.paramstyle=None¶ – The paramstyle to use when rendering bound parameters. This style defaults to the one recommended by the DBAPI itself, which is retrieved from the
.paramstyle
attribute of the DBAPI. However, most DBAPIs accept more than one paramstyle, and in particular it may be desirable to change a “named” paramstyle into a “positional” one, or vice versa. When this attribute is passed, it should be one of the values"qmark"
,"numeric"
,"named"
,"format"
or"pyformat"
, and should correspond to a parameter style known to be supported by the DBAPI in use.pool=None¶ – an already-constructed instance of
Pool
, such as aQueuePool
instance. If non-None, this pool will be used directly as the underlying connection pool for the engine, bypassing whatever connection parameters are present in the URL argument. For information on constructing connection pools manually, see 连接池.poolclass=None¶ – a
Pool
subclass, which will be used to create a connection pool instance using the connection parameters given in the URL. Note this differs frompool
in that you don’t actually instantiate the pool in this case, you just indicate what type of pool to be used.pool_logging_name¶ –
String identifier which will be used within the “name” field of logging records generated within the “sqlalchemy.pool” logger. Defaults to a hexstring of the object’s id.
参见
配置日志记录 - further detail on how to configure logging.
pool_pre_ping¶ –
boolean, if True will enable the connection pool “pre-ping” feature that tests connections for liveness upon each checkout.
参见
pool_size=5¶ – the number of connections to keep open inside the connection pool. This used with
QueuePool
as well asSingletonThreadPool
. WithQueuePool
, apool_size
setting of 0 indicates no limit; to disable pooling, setpoolclass
toNullPool
instead.pool_recycle=-1¶ –
this setting causes the pool to recycle connections after the given number of seconds has passed. It defaults to -1, or no timeout. For example, setting to 3600 means connections will be recycled after one hour. Note that MySQL in particular will disconnect automatically if no activity is detected on a connection for eight hours (although this is configurable with the MySQLDB connection itself and the server configuration as well).
参见
pool_reset_on_return='rollback'¶ –
set the
Pool.reset_on_return
parameter of the underlyingPool
object, which can be set to the values"rollback"
,"commit"
, orNone
.参见
pool_timeout=30¶ –
number of seconds to wait before giving up on getting a connection from the pool. This is only used with
QueuePool
. This can be a float but is subject to the limitations of Python time functions which may not be reliable in the tens of milliseconds.pool_use_lifo=False¶ –
use LIFO (last-in-first-out) when retrieving connections from
QueuePool
instead of FIFO (first-in-first-out). Using LIFO, a server-side timeout scheme can reduce the number of connections used during non- peak periods of use. When planning for server-side timeouts, ensure that a recycle or pre-ping strategy is in use to gracefully handle stale connections.plugins¶ – string list of plugin names to load. See
CreateEnginePlugin
for background.query_cache_size¶ –
size of the cache used to cache the SQL string form of queries. Set to zero to disable caching.
The cache is pruned of its least recently used items when its size reaches N * 1.5. Defaults to 500, meaning the cache will always store at least 500 SQL statements when filled, and will grow up to 750 items at which point it is pruned back down to 500 by removing the 250 least recently used items.
Caching is accomplished on a per-statement basis by generating a cache key that represents the statement’s structure, then generating string SQL for the current dialect only if that key is not present in the cache. All statements support caching, however some features such as an INSERT with a large set of parameters will intentionally bypass the cache. SQL logging will indicate statistics for each statement whether or not it were pull from the cache.
备注
some ORM functions related to unit-of-work persistence as well as some attribute loading strategies will make use of individual per-mapper caches outside of the main cache.
参见
在 1.4 版本加入.
use_insertmanyvalues¶ –
True by default, use the “insertmanyvalues” execution style for INSERT..RETURNING statements by default.
在 2.0 版本加入.
- function sqlalchemy.engine_from_config(configuration: Dict[str, Any], prefix: str = 'sqlalchemy.', **kwargs: Any) Engine ¶
Create a new Engine instance using a configuration dictionary.
The dictionary is typically produced from a config file.
The keys of interest to
engine_from_config()
should be prefixed, e.g.sqlalchemy.url
,sqlalchemy.echo
, etc. The ‘prefix’ argument indicates the prefix to be searched for. Each matching key (after the prefix is stripped) is treated as though it were the corresponding keyword argument to acreate_engine()
call.The only required key is (assuming the default prefix)
sqlalchemy.url
, which provides the database URL.A select set of keyword arguments will be “coerced” to their expected type based on string values. The set of arguments is extensible per-dialect using the
engine_config_types
accessor.- 参数:
configuration¶ – A dictionary (typically produced from a config file, but this is not a requirement). Items whose keys start with the value of ‘prefix’ will have that prefix stripped, and will then be passed to
create_engine()
.prefix¶ – Prefix to match and then strip from keys in ‘configuration’.
kwargs¶ – Each keyword argument to
engine_from_config()
itself overrides the corresponding item taken from the ‘configuration’ dictionary. Keyword arguments should not be prefixed.
- function sqlalchemy.create_mock_engine(url: str | URL, executor: Any, **kw: Any) MockConnection ¶
Create a “mock” engine used for echoing DDL.
This is a utility function used for debugging or storing the output of DDL sequences as generated by
MetaData.create_all()
and related methods.The function accepts a URL which is used only to determine the kind of dialect to be used, as well as an “executor” callable function which will receive a SQL expression object and parameters, which can then be echoed or otherwise printed. The executor’s return value is not handled, nor does the engine allow regular string statements to be invoked, and is therefore only useful for DDL that is sent to the database without receiving any results.
E.g.:
from sqlalchemy import create_mock_engine def dump(sql, *multiparams, **params): print(sql.compile(dialect=engine.dialect)) engine = create_mock_engine("postgresql+psycopg2://", dump) metadata.create_all(engine, checkfirst=False)
- 参数:
url¶ – A string URL which typically needs to contain only the database backend name.
executor¶ – a callable which receives the arguments
sql
,*multiparams
and**params
. Thesql
parameter is typically an instance ofExecutableDDLElement
, which can then be compiled into a string usingExecutableDDLElement.compile()
.
在 1.4 版本加入: - the
create_mock_engine()
function replaces the previous “mock” engine strategy used withcreate_engine()
.
- function sqlalchemy.engine.make_url(name_or_url: str | URL) URL ¶
Given a string, produce a new URL instance.
The format of the URL generally follows RFC-1738, with some exceptions, including that underscores, and not dashes or periods, are accepted within the “scheme” portion.
If a
URL
object is passed, it is returned as is.参见
- function sqlalchemy.create_pool_from_url(url: str | URL, **kwargs: Any) Pool ¶
Create a pool instance from the given url.
If
poolclass
is not provided the pool class used is selected using the dialect specified in the URL.The arguments passed to
create_pool_from_url()
are identical to the pool argument passed to thecreate_engine()
function.在 2.0.10 版本加入.
- class sqlalchemy.engine.URL¶
Represent the components of a URL used to connect to a database.
URLs are typically constructed from a fully formatted URL string, where the
make_url()
function is used internally by thecreate_engine()
function in order to parse the URL string into its individual components, which are then used to construct a newURL
object. When parsing from a formatted URL string, the parsing format generally follows RFC-1738, with some exceptions.A
URL
object may also be produced directly, either by using themake_url()
function with a fully formed URL string, or by using theURL.create()
constructor in order to construct aURL
programmatically given individual fields. The resultingURL
object may be passed directly tocreate_engine()
in place of a string argument, which will bypass the usage ofmake_url()
within the engine’s creation process.在 1.4 版本发生变更: The
URL
object is now an immutable object. To create a URL, use themake_url()
orURL.create()
function / method. To modify aURL
, use methods likeURL.set()
andURL.update_query_dict()
to return a newURL
object with modifications. See notes for this change at The URL object is now immutable.参见
URL
contains the following attributes:URL.drivername
: database backend and driver name, such aspostgresql+psycopg2
URL.username
: username stringURL.password
: password stringURL.host
: string hostnameURL.port
: integer port numberURL.database
: string database nameURL.query
: an immutable mapping representing the query string. contains strings for keys and either strings or tuples of strings for values.
Members
create(), database, difference_update_query(), drivername, get_backend_name(), get_dialect(), get_driver_name(), host, normalized_query, password, port, query, render_as_string(), set(), translate_connect_args(), update_query_dict(), update_query_pairs(), update_query_string(), username
Class signature
class
sqlalchemy.engine.URL
(builtins.tuple
)-
classmethod
sqlalchemy.engine.URL.
create(drivername: str, username: str | None = None, password: str | None = None, host: str | None = None, port: int | None = None, database: str | None = None, query: Mapping[str, Sequence[str] | str] = {}) URL ¶ Create a new
URL
object.参见
- 参数:
drivername¶ – the name of the database backend. This name will correspond to a module in sqlalchemy/databases or a third party plug-in.
username¶ – The user name.
password¶ –
database password. Is typically a string, but may also be an object that can be stringified with
str()
.备注
The password string should not be URL encoded when passed as an argument to
URL.create()
; the string should contain the password characters exactly as they would be typed.备注
A password-producing object will be stringified only once per
Engine
object. For dynamic password generation per connect, see 生成动态身份验证令牌.host¶ – The name of the host.
port¶ – The port number.
database¶ – The database name.
query¶ – A dictionary of string keys to string values to be passed to the dialect and/or the DBAPI upon connect. To specify non-string parameters to a Python DBAPI directly, use the
create_engine.connect_args
parameter tocreate_engine()
. See alsoURL.normalized_query
for a dictionary that is consistently string->list of string.
- 返回:
new
URL
object.
在 1.4 版本加入: The
URL
object is now an immutable named tuple. In addition, thequery
dictionary is also immutable. To create a URL, use themake_url()
orURL.create()
function/ method. To modify aURL
, use theURL.set()
andURL.update_query()
methods.
-
attribute
sqlalchemy.engine.URL.
database: str | None¶ database name
-
method
sqlalchemy.engine.URL.
difference_update_query(names: Iterable[str]) URL ¶ Remove the given names from the
URL.query
dictionary, returning the newURL
.E.g.:
url = url.difference_update_query(["foo", "bar"])
Equivalent to using
URL.set()
as follows:url = url.set( query={ key: url.query[key] for key in set(url.query).difference(["foo", "bar"]) } )
在 1.4 版本加入.
-
attribute
sqlalchemy.engine.URL.
drivername: str¶ database backend and driver name, such as
postgresql+psycopg2
-
method
sqlalchemy.engine.URL.
get_backend_name() str ¶ Return the backend name.
This is the name that corresponds to the database backend in use, and is the portion of the
URL.drivername
that is to the left of the plus sign.
-
method
sqlalchemy.engine.URL.
get_dialect(_is_async: bool = False) Type[Dialect] ¶ Return the SQLAlchemy
Dialect
class corresponding to this URL’s driver name.
-
method
sqlalchemy.engine.URL.
get_driver_name() str ¶ Return the backend name.
This is the name that corresponds to the DBAPI driver in use, and is the portion of the
URL.drivername
that is to the right of the plus sign.If the
URL.drivername
does not include a plus sign, then the defaultDialect
for thisURL
is imported in order to get the driver name.
-
attribute
sqlalchemy.engine.URL.
host: str | None¶ hostname or IP number. May also be a data source name for some drivers.
-
attribute
sqlalchemy.engine.URL.
normalized_query¶ Return the
URL.query
dictionary with values normalized into sequences.As the
URL.query
dictionary may contain either string values or sequences of string values to differentiate between parameters that are specified multiple times in the query string, code that needs to handle multiple parameters generically will wish to use this attribute so that all parameters present are presented as sequences. Inspiration is from Python’surllib.parse.parse_qs
function. E.g.:>>> from sqlalchemy.engine import make_url >>> url = make_url( ... "postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt" ... ) >>> url.query immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': '/path/to/crt'}) >>> url.normalized_query immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': ('/path/to/crt',)})
-
attribute
sqlalchemy.engine.URL.
password: str | None¶ password, which is normally a string but may also be any object that has a
__str__()
method.
-
attribute
sqlalchemy.engine.URL.
port: int | None¶ integer port number
-
attribute
sqlalchemy.engine.URL.
query: immutabledict¶ an immutable mapping representing the query string. contains strings for keys and either strings or tuples of strings for values, e.g.:
>>> from sqlalchemy.engine import make_url >>> url = make_url( ... "postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt" ... ) >>> url.query immutabledict({'alt_host': ('host1', 'host2'), 'ssl_cipher': '/path/to/crt'}) To create a mutable copy of this mapping, use the ``dict`` constructor:: mutable_query_opts = dict(url.query)
参见
URL.normalized_query
- normalizes all values into sequences for consistent processingMethods for altering the contents of
URL.query
:
-
method
sqlalchemy.engine.URL.
render_as_string(hide_password: bool = True) str ¶ Render this
URL
object as a string.This method is used when the
__str__()
or__repr__()
methods are used. The method directly includes additional options.- 参数:
hide_password¶ – Defaults to True. The password is not shown in the string unless this is set to False.
-
method
sqlalchemy.engine.URL.
set(drivername: str | None = None, username: str | None = None, password: str | None = None, host: str | None = None, port: int | None = None, database: str | None = None, query: Mapping[str, Sequence[str] | str] | None = None) URL ¶ return a new
URL
object with modifications.Values are used if they are non-None. To set a value to
None
explicitly, use theURL._replace()
method adapted fromnamedtuple
.- 参数:
- 返回:
new
URL
object.
在 1.4 版本加入.
-
method
sqlalchemy.engine.URL.
translate_connect_args(names: List[str] | None = None, **kw: Any) Dict[str, Any] ¶ Translate url attributes into a dictionary of connection arguments.
Returns attributes of this url (host, database, username, password, port) as a plain dictionary. The attribute names are used as the keys by default. Unset or false attributes are omitted from the final dictionary.
-
method
sqlalchemy.engine.URL.
update_query_dict(query_parameters: Mapping[str, str | List[str]], append: bool = False) URL ¶ Return a new
URL
object with theURL.query
parameter dictionary updated by the given dictionary.The dictionary typically contains string keys and string values. In order to represent a query parameter that is expressed multiple times, pass a sequence of string values.
E.g.:
>>> from sqlalchemy.engine import make_url >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname") >>> url = url.update_query_dict( ... {"alt_host": ["host1", "host2"], "ssl_cipher": "/path/to/crt"} ... ) >>> str(url) 'postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt'
- 参数:
query_parameters¶ – A dictionary with string keys and values that are either strings, or sequences of strings.
append¶ – if True, parameters in the existing query string will not be removed; new parameters will be in addition to those present. If left at its default of False, keys present in the given query parameters will replace those of the existing query string.
在 1.4 版本加入.
-
method
sqlalchemy.engine.URL.
update_query_pairs(key_value_pairs: Iterable[Tuple[str, str | List[str]]], append: bool = False) URL ¶ Return a new
URL
object with theURL.query
parameter dictionary updated by the given sequence of key/value pairsE.g.:
>>> from sqlalchemy.engine import make_url >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname") >>> url = url.update_query_pairs( ... [ ... ("alt_host", "host1"), ... ("alt_host", "host2"), ... ("ssl_cipher", "/path/to/crt"), ... ] ... ) >>> str(url) 'postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt'
- 参数:
key_value_pairs¶ – A sequence of tuples containing two strings each.
append¶ – if True, parameters in the existing query string will not be removed; new parameters will be in addition to those present. If left at its default of False, keys present in the given query parameters will replace those of the existing query string.
在 1.4 版本加入.
-
method
sqlalchemy.engine.URL.
update_query_string(query_string: str, append: bool = False) URL ¶ Return a new
URL
object with theURL.query
parameter dictionary updated by the given query string.E.g.:
>>> from sqlalchemy.engine import make_url >>> url = make_url("postgresql+psycopg2://user:pass@host/dbname") >>> url = url.update_query_string( ... "alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt" ... ) >>> str(url) 'postgresql+psycopg2://user:pass@host/dbname?alt_host=host1&alt_host=host2&ssl_cipher=%2Fpath%2Fto%2Fcrt'
- 参数:
query_string¶ – a URL escaped query string, not including the question mark.
append¶ – if True, parameters in the existing query string will not be removed; new parameters will be in addition to those present. If left at its default of False, keys present in the given query parameters will replace those of the existing query string.
在 1.4 版本加入.
-
attribute
sqlalchemy.engine.URL.
username: str | None¶ username string
连接池¶
Pooling
当调用 connect()
或 execute()
方法时,Engine
会向连接池请求一个连接。默认的连接池 QueuePool
会根据需要建立与数据库的连接。随着并发语句的执行,QueuePool
会将其连接池扩展到默认的五个连接,并允许默认“溢出”数量为十个。由于 Engine
本质上是连接池的“主控中心”,因此应该在应用中为每个数据库仅创建一个 Engine
实例,而不是为每次连接都新建一个实例。
有关连接池的更多信息,请参阅 连接池。
The Engine
will ask the connection pool for a connection when the connect()
or execute()
methods are called. The default connection pool, QueuePool
, will open connections to the database on an as-needed basis. As concurrent statements are executed, QueuePool
will grow its pool of connections to a default size of five, and will allow a default “overflow” of ten. Since the Engine
is essentially “home base” for the connection pool, it follows that you should keep a single Engine
per database established within an application, rather than creating a new one for each connection.
备注
QueuePool
is not used by default for SQLite engines. See
SQLite for details on SQLite connection pool usage.
For more information on connection pooling, see 连接池.
自定义 DBAPI connect() 参数/连接例程¶
Custom DBAPI connect() arguments / on-connect routines
在需要特殊连接方式的场景中,在绝大多数情况下,最合适的做法是使用 create_engine()
提供的多个钩子之一来自定义连接过程。相关内容将在后续小节中介绍。
For cases where special connection methods are needed, in the vast majority of cases, it is most appropriate to use one of several hooks at the create_engine()
level in order to customize this process. These are described in the following sub-sections.
传递给 dbapi.connect() 的特殊关键字参数¶
Special Keyword Arguments Passed to dbapi.connect()
所有 Python DBAPI 都接受除基本连接参数外的附加参数。常见的参数包括用于指定字符集编码和超时值的选项;更复杂的则可能包括特殊的 DBAPI 常量、对象及 SSL 子参数。有两种基本方式可以简单地传递这些参数。
All Python DBAPIs accept additional arguments beyond the basics of connecting. Common parameters include those to specify character set encodings and timeout values; more complex data includes special DBAPI constants and objects and SSL sub-parameters. There are two rudimentary means of passing these arguments without complexity.
将参数添加到 URL 查询字符串¶
Add Parameters to the URL Query string
简单的字符串值,以及部分数值和布尔标志,通常可以直接通过 URL 的查询字符串传递。一个常见示例是 DBAPI 接受 encoding
参数来指定字符编码,例如大多数 MySQL 的 DBAPI 实现:
engine = create_engine("mysql+pymysql://user:pass@host/test?charset=utf8mb4")
使用查询字符串的优点是可以将附加的 DBAPI 选项以一种对 URL 中指定的 DBAPI 具有可移植性的方式写入配置文件。在此层级传递的具体参数会因 SQLAlchemy 方言(dialect)而异。有些方言会将所有参数按字符串形式传递,而另一些方言会对特定的数据类型进行解析,并将参数移至不同的位置,例如驱动层级的 DSN 或连接字符串。由于每种方言在此方面的行为目前仍存在差异,应查阅所使用方言的文档,以确认特定参数是否在该层级被支持。
小技巧
一种通用技巧可用于查看为某个 URL 实际传递给 DBAPI 的参数,
可通过直接调用 Dialect.create_connect_args()
方法来实现:
>>> from sqlalchemy import create_engine
>>> engine = create_engine(
... "mysql+pymysql://some_user:some_pass@some_host/test?charset=utf8mb4"
... )
>>> args, kwargs = engine.dialect.create_connect_args(engine.url)
>>> args, kwargs
([], {'host': 'some_host', 'database': 'test', 'user': 'some_user', 'password': 'some_pass', 'charset': 'utf8mb4', 'client_flag': 2})
上述 args, kwargs
对通常以 dbapi.connect(*args, **kwargs)
的形式传递给 DBAPI。
Simple string values, as well as some numeric values and boolean flags, may be
often specified in the query string of the URL directly. A common example of
this is DBAPIs that accept an argument encoding
for character encodings,
such as most MySQL DBAPIs:
engine = create_engine("mysql+pymysql://user:pass@host/test?charset=utf8mb4")
The advantage of using the query string is that additional DBAPI options may be specified in configuration files in a manner that’s portable to the DBAPI specified in the URL. The specific parameters passed through at this level vary by SQLAlchemy dialect. Some dialects pass all arguments through as strings, while others will parse for specific datatypes and move parameters to different places, such as into driver-level DSNs and connect strings. As per-dialect behavior in this area currently varies, the dialect documentation should be consulted for the specific dialect in use to see if particular parameters are supported at this level.
小技巧
A general technique to display the exact arguments passed to the DBAPI
for a given URL may be performed using the Dialect.create_connect_args()
method directly as follows:
>>> from sqlalchemy import create_engine
>>> engine = create_engine(
... "mysql+pymysql://some_user:some_pass@some_host/test?charset=utf8mb4"
... )
>>> args, kwargs = engine.dialect.create_connect_args(engine.url)
>>> args, kwargs
([], {'host': 'some_host', 'database': 'test', 'user': 'some_user', 'password': 'some_pass', 'charset': 'utf8mb4', 'client_flag': 2})
The above args, kwargs
pair is normally passed to the DBAPI as
dbapi.connect(*args, **kwargs)
.
使用 connect_args 字典参数¶
Use the connect_args dictionary parameter
一个更通用的方式,可以将任何参数传递给 dbapi.connect()
函数,并且可以确保在任何时候都传递所有参数,即使用 create_engine.connect_args
字典参数。该参数适用于以下场景:当通过查询字符串添加参数时无法被方言识别,或者需要将特殊的子结构或对象传递给 DBAPI。有时,仅仅是某个特定的标志需要以 True
的形式传递,而 SQLAlchemy 的方言并不知道要将其从 URL 中的字符串形式转换为布尔值。以下示例展示了如何使用 psycopg2 的 “连接工厂” 替代底层连接实现:
engine = create_engine(
"postgresql+psycopg2://user:pass@hostname/dbname",
connect_args={"connection_factory": MyConnectionFactory},
)
另一个示例是 pyodbc 的 “timeout” 参数:
engine = create_engine(
"mssql+pyodbc://user:pass@sqlsrvr?driver=ODBC+Driver+13+for+SQL+Server",
connect_args={"timeout": 30},
)
上述示例还说明,URL 查询字符串参数和 create_engine.connect_args
参数可以同时使用;对于 pyodbc 来说,URL 中的 “driver” 关键字具有特殊含义。
A more general system of passing any parameter to the dbapi.connect()
function that is guaranteed to pass all parameters at all times is the
create_engine.connect_args
dictionary parameter. This may be
used for parameters that are otherwise not handled by the dialect when added to
the query string, as well as when special sub-structures or objects must be
passed to the DBAPI. Sometimes it’s just that a particular flag must be sent as
the True
symbol and the SQLAlchemy dialect is not aware of this keyword
argument to coerce it from its string form as presented in the URL. Below
illustrates the use of a psycopg2 “connection factory” that replaces the
underlying implementation the connection:
engine = create_engine(
"postgresql+psycopg2://user:pass@hostname/dbname",
connect_args={"connection_factory": MyConnectionFactory},
)
Another example is the pyodbc “timeout” parameter:
engine = create_engine(
"mssql+pyodbc://user:pass@sqlsrvr?driver=ODBC+Driver+13+for+SQL+Server",
connect_args={"timeout": 30},
)
The above example also illustrates that both URL “query string” parameters as
well as create_engine.connect_args
may be used at the same
time; in the case of pyodbc, the “driver” keyword has special meaning
within the URL.
控制如何将参数传递给 DBAPI connect() 函数¶
Controlling how parameters are passed to the DBAPI connect() function
除了操作传递给 connect()
的参数之外,我们还可以进一步自定义调用 DBAPI 的 connect()
函数的方式,方法是使用 DialectEvents.do_connect()
事件钩子。该钩子接收方言将发送给 connect()
的完整 *args, **kwargs
。你可以在钩子中就地修改这些参数集合,以改变它们的使用方式:
from sqlalchemy import event
engine = create_engine("postgresql+psycopg2://user:pass@hostname/dbname")
@event.listens_for(engine, "do_connect")
def receive_do_connect(dialect, conn_rec, cargs, cparams):
cparams["connection_factory"] = MyConnectionFactory
Beyond manipulating the parameters passed to connect()
, we can further
customize how the DBAPI connect()
function itself is called using the
DialectEvents.do_connect()
event hook. This hook is passed the full
*args, **kwargs
that the dialect would send to connect()
. These
collections can then be modified in place to alter how they are used:
from sqlalchemy import event
engine = create_engine("postgresql+psycopg2://user:pass@hostname/dbname")
@event.listens_for(engine, "do_connect")
def receive_do_connect(dialect, conn_rec, cargs, cparams):
cparams["connection_factory"] = MyConnectionFactory
生成动态身份验证令牌¶
Generating dynamic authentication tokens
DialectEvents.do_connect()
也是在 Engine
生命周期内动态插入认证令牌的理想方式。例如,如果令牌是通过 get_authentication_token()
生成的,并通过 token
参数传递给 DBAPI,可以这样实现:
from sqlalchemy import event
engine = create_engine("postgresql+psycopg2://user@hostname/dbname")
@event.listens_for(engine, "do_connect")
def provide_token(dialect, conn_rec, cargs, cparams):
cparams["token"] = get_authentication_token()
参见
使用访问令牌连接到数据库 - 一个更具体的 SQL Server 示例
DialectEvents.do_connect()
is also an ideal way to dynamically
insert an authentication token that might change over the lifespan of an
Engine
. For example, if the token gets generated by
get_authentication_token()
and passed to the DBAPI in a token
parameter, this could be implemented as:
from sqlalchemy import event
engine = create_engine("postgresql+psycopg2://user@hostname/dbname")
@event.listens_for(engine, "do_connect")
def provide_token(dialect, conn_rec, cargs, cparams):
cparams["token"] = get_authentication_token()
参见
使用访问令牌连接到数据库 - a more concrete example involving SQL Server
在连接后修改 DBAPI 连接,或在连接后运行命令¶
Modifying the DBAPI connection after connect, or running commands after connect
如果 SQLAlchemy 能够成功创建 DBAPI 连接,但你希望在连接实际使用之前对其进行修改(例如设置特殊标志或执行某些命令),那么 PoolEvents.connect()
事件钩子是最合适的选择。该钩子在每个新连接创建后被调用,并且是在连接被 SQLAlchemy 使用之前:
from sqlalchemy import event
engine = create_engine("postgresql+psycopg2://user:pass@hostname/dbname")
@event.listens_for(engine, "connect")
def connect(dbapi_connection, connection_record):
cursor_obj = dbapi_connection.cursor()
cursor_obj.execute("SET some session variables")
cursor_obj.close()
For a DBAPI connection that SQLAlchemy creates without issue, but where we
would like to modify the completed connection before it’s actually used, such
as for setting special flags or running certain commands, the
PoolEvents.connect()
event hook is the most appropriate hook. This
hook is called for every new connection created, before it is used by
SQLAlchemy:
from sqlalchemy import event
engine = create_engine("postgresql+psycopg2://user:pass@hostname/dbname")
@event.listens_for(engine, "connect")
def connect(dbapi_connection, connection_record):
cursor_obj = dbapi_connection.cursor()
cursor_obj.execute("SET some session variables")
cursor_obj.close()
完全替换 DBAPI connect()
函数¶
Fully Replacing the DBAPI connect()
function
最后,DialectEvents.do_connect()
事件钩子还允许我们完全接管连接过程,自己建立连接并返回连接:
from sqlalchemy import event
engine = create_engine("postgresql+psycopg2://user:pass@hostname/dbname")
@event.listens_for(engine, "do_connect")
def receive_do_connect(dialect, conn_rec, cargs, cparams):
# 返回我们想要的新的 DBAPI 连接
return psycopg2.connect(*cargs, **cparams)
DialectEvents.do_connect()
钩子优于之前的 create_engine.creator
钩子,尽管后者仍然可用。DialectEvents.do_connect()
的一个显著优点是,URL 解析出的完整参数也会传递给用户定义的函数,而 create_engine.creator
并没有做到这一点。
Finally, the DialectEvents.do_connect()
event hook can also allow us to take
over the connection process entirely by establishing the connection
and returning it:
from sqlalchemy import event
engine = create_engine("postgresql+psycopg2://user:pass@hostname/dbname")
@event.listens_for(engine, "do_connect")
def receive_do_connect(dialect, conn_rec, cargs, cparams):
# return the new DBAPI connection with whatever we'd like to
# do
return psycopg2.connect(*cargs, **cparams)
The DialectEvents.do_connect()
hook supersedes the previous
create_engine.creator
hook, which remains available.
DialectEvents.do_connect()
has the distinct advantage that the
complete arguments parsed from the URL are also passed to the user-defined
function which is not the case with create_engine.creator
.
配置日志记录¶
Configuring Logging
Python 的标准 logging 模块用于实现 SQLAlchemy 的信息和调试日志输出。这使得 SQLAlchemy 的日志能够以标准方式与其他应用程序和库集成。还存在两个参数 create_engine.echo
和 create_engine.echo_pool
,它们可以在 create_engine()
上使用,用于将日志直接输出到 sys.stdout
,以便本地开发使用;这些参数最终与下面描述的常规 Python 日志记录器进行交互。
本节假设读者已经熟悉上述链接的日志模块。SQLAlchemy 执行的所有日志记录都位于 sqlalchemy
命名空间下,使用 logging.getLogger('sqlalchemy')
来访问。当日志记录已被配置(例如通过 logging.basicConfig()
)时,可以打开的 SA 日志记录器的一般命名空间如下:
sqlalchemy.engine
- 控制 SQL 回显。设置为logging.INFO
用于 SQL 查询输出,设置为logging.DEBUG
用于查询和结果集输出。这些设置等价于create_engine.echo
上的echo=True
和echo="debug"
。sqlalchemy.pool
- 控制连接池日志。设置为logging.INFO
以记录连接失效和回收事件;设置为logging.DEBUG
以同时记录所有池的检查和归还事件。这些设置等价于create_engine.echo_pool
上的pool_echo=True
和pool_echo="debug"
。sqlalchemy.dialects
- 控制 SQL 方言的自定义日志记录,具体取决于特定方言中是否使用日志记录,通常是最少的。sqlalchemy.orm
- 控制 ORM 函数的日志记录,具体取决于 ORM 中是否使用日志记录,通常也是最少的。设置为logging.INFO
以记录一些有关映射器配置的顶级信息。
例如,使用 Python 日志记录来记录 SQL 查询,而不是使用 echo=True
标志:
import logging
logging.basicConfig()
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
默认情况下,整个 sqlalchemy
命名空间的日志级别设置为 logging.WARN
,因此不会执行任何日志操作,即使应用程序启用了其他日志记录。
备注
SQLAlchemy 的 Engine
会通过仅在当前日志级别被检测为 logging.INFO
或 logging.DEBUG
时才发出日志语句,从而节省 Python 函数调用的开销。它仅在从连接池中获取新连接时检查此级别。因此,在更改已经运行的应用程序的日志配置时,任何当前活动的 Connection
,或更常见的 Session
对象,直到获取新连接后(在 Session
中,这是在当前事务结束并开始新的事务之后)才会根据新配置记录 SQL。
Python’s standard logging module is used to
implement informational and debug log output with SQLAlchemy. This allows
SQLAlchemy’s logging to integrate in a standard way with other applications
and libraries. There are also two parameters
create_engine.echo
and create_engine.echo_pool
present on create_engine()
which allow immediate logging to sys.stdout
for the purposes of local development; these parameters ultimately interact
with the regular Python loggers described below.
This section assumes familiarity with the above linked logging module. All
logging performed by SQLAlchemy exists underneath the sqlalchemy
namespace, as used by logging.getLogger('sqlalchemy')
. When logging has
been configured (i.e. such as via logging.basicConfig()
), the general
namespace of SA loggers that can be turned on is as follows:
sqlalchemy.engine
- controls SQL echoing. Set tologging.INFO
for SQL query output,logging.DEBUG
for query + result set output. These settings are equivalent toecho=True
andecho="debug"
oncreate_engine.echo
, respectively.sqlalchemy.pool
- controls connection pool logging. Set tologging.INFO
to log connection invalidation and recycle events; set tologging.DEBUG
to additionally log all pool checkins and checkouts. These settings are equivalent topool_echo=True
andpool_echo="debug"
oncreate_engine.echo_pool
, respectively.sqlalchemy.dialects
- controls custom logging for SQL dialects, to the extent that logging is used within specific dialects, which is generally minimal.sqlalchemy.orm
- controls logging of various ORM functions to the extent that logging is used within the ORM, which is generally minimal. Set tologging.INFO
to log some top-level information on mapper configurations.
For example, to log SQL queries using Python logging instead of the
echo=True
flag:
import logging
logging.basicConfig()
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
By default, the log level is set to logging.WARN
within the entire
sqlalchemy
namespace so that no log operations occur, even within an
application that has logging enabled otherwise.
备注
The SQLAlchemy Engine
conserves Python function call
overhead by only emitting log statements when the current logging level is
detected as logging.INFO
or logging.DEBUG
. It only checks this
level when a new connection is procured from the connection pool. Therefore
when changing the logging configuration for an already-running application,
any Connection
that’s currently active, or more commonly a
Session
object that’s active in a transaction, won’t
log any SQL according to the new configuration until a new
Connection
is procured (in the case of
Session
, this is after the current transaction ends
and a new one begins).
有关 Echo 标志的更多信息¶
More on the Echo Flag
如前所述,create_engine.echo
和 create_engine.echo_pool
参数是立即将日志记录到 sys.stdout
的快捷方式:
>>> from sqlalchemy import create_engine, text
>>> e = create_engine("sqlite://", echo=True, echo_pool="debug")
>>> with e.connect() as conn:
... print(conn.scalar(text("select 'hi'")))
2020-10-24 12:54:57,701 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Created new connection <sqlite3.Connection object at 0x7f287819ac60>
2020-10-24 12:54:57,701 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f287819ac60> checked out from pool
2020-10-24 12:54:57,702 INFO sqlalchemy.engine.Engine select 'hi'
2020-10-24 12:54:57,702 INFO sqlalchemy.engine.Engine ()
hi
2020-10-24 12:54:57,703 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f287819ac60> being returned to pool
2020-10-24 12:54:57,704 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f287819ac60> rollback-on-return
使用这些标志的效果大致等同于:
import logging
logging.basicConfig()
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
logging.getLogger("sqlalchemy.pool").setLevel(logging.DEBUG)
需要注意的是,这两个标志 独立于 任何现有的日志配置,并且会无条件地使用 logging.basicConfig()
。这会导致在任何现有的日志配置之外进行配置。因此, 在显式配置日志时,请确保所有 echo 标志始终设置为 False,以避免出现重复的日志行。
As mentioned previously, the create_engine.echo
and create_engine.echo_pool
parameters are a shortcut to immediate logging to sys.stdout
:
>>> from sqlalchemy import create_engine, text
>>> e = create_engine("sqlite://", echo=True, echo_pool="debug")
>>> with e.connect() as conn:
... print(conn.scalar(text("select 'hi'")))
2020-10-24 12:54:57,701 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Created new connection <sqlite3.Connection object at 0x7f287819ac60>
2020-10-24 12:54:57,701 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f287819ac60> checked out from pool
2020-10-24 12:54:57,702 INFO sqlalchemy.engine.Engine select 'hi'
2020-10-24 12:54:57,702 INFO sqlalchemy.engine.Engine ()
hi
2020-10-24 12:54:57,703 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f287819ac60> being returned to pool
2020-10-24 12:54:57,704 DEBUG sqlalchemy.pool.impl.SingletonThreadPool Connection <sqlite3.Connection object at 0x7f287819ac60> rollback-on-return
Use of these flags is roughly equivalent to:
import logging
logging.basicConfig()
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
logging.getLogger("sqlalchemy.pool").setLevel(logging.DEBUG)
It’s important to note that these two flags work independently of any
existing logging configuration, and will make use of logging.basicConfig()
unconditionally. This has the effect of being configured in addition to
any existing logger configurations. Therefore, when configuring logging
explicitly, ensure all echo flags are set to False at all times, to avoid
getting duplicate log lines.
设置日志记录名称¶
Setting the Logging Name
Engine
或 Pool
的日志记录器名称设置为对象的模块限定类名。这个名称可以通过 create_engine.logging_name
和 create_engine.pool_logging_name
参数与 sqlalchemy.create_engine()
一起进一步限定;该名称将附加到现有的类限定日志记录名称上。建议在同时使用多个全局 Engine
实例的应用程序中使用此功能,以便可以在日志中区分它们:
>>> import logging
>>> from sqlalchemy import create_engine
>>> from sqlalchemy import text
>>> logging.basicConfig()
>>> logging.getLogger("sqlalchemy.engine.Engine.myengine").setLevel(logging.INFO)
>>> e = create_engine("sqlite://", logging_name="myengine")
>>> with e.connect() as conn:
... conn.execute(text("select 'hi'"))
2020-10-24 12:47:04,291 INFO sqlalchemy.engine.Engine.myengine select 'hi'
2020-10-24 12:47:04,292 INFO sqlalchemy.engine.Engine.myengine ()
小技巧
create_engine.logging_name
和 create_engine.pool_logging_name
参数也可以与 create_engine.echo
和 create_engine.echo_pool
一起使用。然而,如果其他引擎的 echo 标志设置为 True 且 没有 设置日志名称,将不可避免地出现双重日志记录条件。这是因为会自动为 sqlalchemy.engine.Engine
添加一个处理程序,该处理程序将同时记录无名称引擎和具有日志名称的引擎的消息。例如:
from sqlalchemy import create_engine, text
e1 = create_engine("sqlite://", echo=True, logging_name="myname")
with e1.begin() as conn:
conn.execute(text("SELECT 1"))
e2 = create_engine("sqlite://", echo=True)
with e2.begin() as conn:
conn.execute(text("SELECT 2"))
with e1.begin() as conn:
conn.execute(text("SELECT 3"))
上述场景将会重复记录 SELECT 3
。为了解决此问题,确保所有引擎都设置了 logging_name
,或者在不使用 create_engine.echo
和 create_engine.echo_pool
的情况下使用显式的日志记录器/处理程序设置。
The logger name for Engine
or
Pool
is set to be the module-qualified class name of the
object. This name can be further qualified with an additional name
using the
create_engine.logging_name
and
create_engine.pool_logging_name
parameters with
sqlalchemy.create_engine()
; the name will be appended to existing
class-qualified logging name. This use is recommended for applications that
make use of multiple global Engine
instances simultaenously, so
that they may be distinguished in logging:
>>> import logging
>>> from sqlalchemy import create_engine
>>> from sqlalchemy import text
>>> logging.basicConfig()
>>> logging.getLogger("sqlalchemy.engine.Engine.myengine").setLevel(logging.INFO)
>>> e = create_engine("sqlite://", logging_name="myengine")
>>> with e.connect() as conn:
... conn.execute(text("select 'hi'"))
2020-10-24 12:47:04,291 INFO sqlalchemy.engine.Engine.myengine select 'hi'
2020-10-24 12:47:04,292 INFO sqlalchemy.engine.Engine.myengine ()
小技巧
The create_engine.logging_name
and
create_engine.pool_logging_name
parameters may also be used in
conjunction with create_engine.echo
and
create_engine.echo_pool
. However, an unavoidable double logging
condition will occur if other engines are created with echo flags set to True
and no logging name. This is because a handler will be added automatically
for sqlalchemy.engine.Engine
which will log messages both for the name-less
engine as well as engines with logging names. For example:
from sqlalchemy import create_engine, text
e1 = create_engine("sqlite://", echo=True, logging_name="myname")
with e1.begin() as conn:
conn.execute(text("SELECT 1"))
e2 = create_engine("sqlite://", echo=True)
with e2.begin() as conn:
conn.execute(text("SELECT 2"))
with e1.begin() as conn:
conn.execute(text("SELECT 3"))
The above scenario will double log SELECT 3
. To resolve, ensure
all engines have a logging_name
set, or use explicit logger / handler
setup without using create_engine.echo
and
create_engine.echo_pool
.
设置每个连接/子引擎令牌¶
Setting Per-Connection / Sub-Engine Tokens
在 1.4.0b2 版本加入.
尽管日志记录名称适合用于长期存在的 Engine
对象,但它不足以灵活应对具有任意多名称的情况,特别是在需要跟踪单独连接和/或事务的日志消息时。
对于这种使用情况,可以通过 Connection
和 Result
对象生成的日志消息,增加额外的标识符,如事务或请求标识符。Connection.execution_options.logging_token
参数接受一个字符串参数,该参数可用于建立每个连接的跟踪令牌:
>>> from sqlalchemy import create_engine
>>> e = create_engine("sqlite://", echo="debug")
>>> with e.connect().execution_options(logging_token="track1") as conn:
... conn.execute(text("select 1")).all()
2021-02-03 11:48:45,754 INFO sqlalchemy.engine.Engine [track1] select 1
2021-02-03 11:48:45,754 INFO sqlalchemy.engine.Engine [track1] [raw sql] ()
2021-02-03 11:48:45,754 DEBUG sqlalchemy.engine.Engine [track1] Col ('1',)
2021-02-03 11:48:45,755 DEBUG sqlalchemy.engine.Engine [track1] Row (1,)
Connection.execution_options.logging_token
参数也可以通过 create_engine.execution_options
或 Engine.execution_options()
在引擎或子引擎上进行设置。这对于应用不同的日志记录令牌到应用程序的不同组件而不创建新引擎时非常有用:
>>> from sqlalchemy import create_engine
>>> e = create_engine("sqlite://", echo="debug")
>>> e1 = e.execution_options(logging_token="track1")
>>> e2 = e.execution_options(logging_token="track2")
>>> with e1.connect() as conn:
... conn.execute(text("select 1")).all()
2021-02-03 11:51:08,960 INFO sqlalchemy.engine.Engine [track1] select 1
2021-02-03 11:51:08,960 INFO sqlalchemy.engine.Engine [track1] [raw sql] ()
2021-02-03 11:51:08,960 DEBUG sqlalchemy.engine.Engine [track1] Col ('1',)
2021-02-03 11:51:08,961 DEBUG sqlalchemy.engine.Engine [track1] Row (1,)
>>> with e2.connect() as conn:
... conn.execute(text("select 2")).all()
2021-02-03 11:52:05,518 INFO sqlalchemy.engine.Engine [track2] Select 1
2021-02-03 11:52:05,519 INFO sqlalchemy.engine.Engine [track2] [raw sql] ()
2021-02-03 11:52:05,520 DEBUG sqlalchemy.engine.Engine [track2] Col ('1',)
2021-02-03 11:52:05,520 DEBUG sqlalchemy.engine.Engine [track2] Row (1,)
While the logging name is appropriate to establish on an
Engine
object that is long lived, it’s not flexible enough
to accommodate for an arbitrarily large list of names, for the case of
tracking individual connections and/or transactions in log messages.
For this use case, the log message itself generated by the
Connection
and Result
objects may be
augmented with additional tokens such as transaction or request identifiers.
The Connection.execution_options.logging_token
parameter
accepts a string argument that may be used to establish per-connection tracking
tokens:
>>> from sqlalchemy import create_engine
>>> e = create_engine("sqlite://", echo="debug")
>>> with e.connect().execution_options(logging_token="track1") as conn:
... conn.execute(text("select 1")).all()
2021-02-03 11:48:45,754 INFO sqlalchemy.engine.Engine [track1] select 1
2021-02-03 11:48:45,754 INFO sqlalchemy.engine.Engine [track1] [raw sql] ()
2021-02-03 11:48:45,754 DEBUG sqlalchemy.engine.Engine [track1] Col ('1',)
2021-02-03 11:48:45,755 DEBUG sqlalchemy.engine.Engine [track1] Row (1,)
The Connection.execution_options.logging_token
parameter
may also be established on engines or sub-engines via
create_engine.execution_options
or Engine.execution_options()
.
This may be useful to apply different logging tokens to different components
of an application without creating new engines:
>>> from sqlalchemy import create_engine
>>> e = create_engine("sqlite://", echo="debug")
>>> e1 = e.execution_options(logging_token="track1")
>>> e2 = e.execution_options(logging_token="track2")
>>> with e1.connect() as conn:
... conn.execute(text("select 1")).all()
2021-02-03 11:51:08,960 INFO sqlalchemy.engine.Engine [track1] select 1
2021-02-03 11:51:08,960 INFO sqlalchemy.engine.Engine [track1] [raw sql] ()
2021-02-03 11:51:08,960 DEBUG sqlalchemy.engine.Engine [track1] Col ('1',)
2021-02-03 11:51:08,961 DEBUG sqlalchemy.engine.Engine [track1] Row (1,)
>>> with e2.connect() as conn:
... conn.execute(text("select 2")).all()
2021-02-03 11:52:05,518 INFO sqlalchemy.engine.Engine [track2] Select 1
2021-02-03 11:52:05,519 INFO sqlalchemy.engine.Engine [track2] [raw sql] ()
2021-02-03 11:52:05,520 DEBUG sqlalchemy.engine.Engine [track2] Col ('1',)
2021-02-03 11:52:05,520 DEBUG sqlalchemy.engine.Engine [track2] Row (1,)
隐藏参数¶
Hiding Parameters
由 Engine
发出的日志也会指示特定语句中存在的 SQL 参数摘录。为了出于隐私目的防止记录这些参数,可以启用 create_engine.hide_parameters
标志:
>>> e = create_engine("sqlite://", echo=True, hide_parameters=True)
>>> with e.connect() as conn:
... conn.execute(text("select :some_private_name"), {"some_private_name": "pii"})
2020-10-24 12:48:32,808 INFO sqlalchemy.engine.Engine select ?
2020-10-24 12:48:32,808 INFO sqlalchemy.engine.Engine [SQL parameters hidden due to hide_parameters=True]
The logging emitted by Engine
also indicates an excerpt
of the SQL parameters that are present for a particular statement. To prevent
these parameters from being logged for privacy purposes, enable the
create_engine.hide_parameters
flag:
>>> e = create_engine("sqlite://", echo=True, hide_parameters=True)
>>> with e.connect() as conn:
... conn.execute(text("select :some_private_name"), {"some_private_name": "pii"})
2020-10-24 12:48:32,808 INFO sqlalchemy.engine.Engine select ?
2020-10-24 12:48:32,808 INFO sqlalchemy.engine.Engine [SQL parameters hidden due to hide_parameters=True]