`sqlalchemy.engine.url.create`访问postgresql数据库的有效`Query'键是什么?
我有兴趣使用
这是参数:
驱动器
:数据库后端的名称。此名称将对应于Sqlalchemy/数据库中的模块或第三方插件。用户名
:用户名密码
:数据库密码。通常是字符串,但也可能是可以用str()
串起的对象。主机
:主机的名称port
:端口号数据库
:数据库名称查询>查询
:字符串键的字符串值要传递到方言和/或 dbapi 连接时。要直接将非弦乐参数指定到Python dbapi,请使用create_engine.connect_args
参数到create_engine()
。另请参阅url.normalized_query
有关字符串字符串列表的字典。
我对IS QUERY
感到困惑的参数。我不知道如何将其用于PostgreSQL。我找到了一个适用于Microsoft SQL的示例:
from sqlalchemy.engine import URL
from sqlalchemy import create_engine
# CONFIG
SERVER_NAME = 'FOO'
DATABASE_NAME = 'BAR'
# QUERY
QUERY = '''SELECT * FROM table'''
# CONNECTION
CONNECTION_STRING = 'Driver={SQL Server};Server={%s};Database=%s;Trusted_Connection=yes;' % (
SERVER_NAME,
DATABASE_NAME
)
CONNECTION_URL = URL.create("mssql+pyodbc",
query={"odbc_connect": CONNECTION_STRING})
CONNECTION = create_engine(CONNECTION_URL)
很明显,我可以将其用于连接字符串中传递。我想我可以用 替换“ mssql+pyodbc”
“ postgresql+psycopg2” ,但我不知道该替换“ odbc_connect”
和。
- (短期)我可以将
查询
更改为PostgreSQL和 - (从根本上)我可以找到详细的文档来理解
查询>查询
参数的可能输入?
I am interested in using the sqlalchemy.engine.URL.create
constructor, but I am finding the documentation to be a too brief on the details to understand how to use it.
Here are the parameters:
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 namepassword
: database password. Is typically a string, but may also be an object that can be stringified withstr()
.host
: The name of the hostport
: The port numberdatabase
: The database namequery
: 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 thecreate_engine.connect_args
parameter tocreate_engine()
. See alsoURL.normalized_query
for a dictionary that is consistently string->list of string.
The parameter I am confused about is query
. I don't know how to use it for PostgreSQL. I found an example that works for Microsoft SQL:
from sqlalchemy.engine import URL
from sqlalchemy import create_engine
# CONFIG
SERVER_NAME = 'FOO'
DATABASE_NAME = 'BAR'
# QUERY
QUERY = '''SELECT * FROM table'''
# CONNECTION
CONNECTION_STRING = 'Driver={SQL Server};Server={%s};Database=%s;Trusted_Connection=yes;' % (
SERVER_NAME,
DATABASE_NAME
)
CONNECTION_URL = URL.create("mssql+pyodbc",
query={"odbc_connect": CONNECTION_STRING})
CONNECTION = create_engine(CONNECTION_URL)
Which makes it evident that I can use it for passing in the connection string. I think I can replace "mssql+pyodbc"
with "postgresql+psycopg2"
, but I don't know what to replace "odbc_connect"
with.
- (short-term) What can I put into
query
to change to PostgreSQL and - (more fundamentally) where can I find detailed documentation for understanding the possible inputs of the
query
parameter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
query =
是我们在连接URL的字符串版本中指定关注?
的内容。 (?
以下值早已被称为URL中的“ querystring”。)odbc_connect
是ODBC连接的一种特殊情况(例如,mssql+pyodbc ://
)。 PostgreSQL没有等效的,因为PostgreSQL
方言的支持驱动程序都没有使用ODBC。所以答案是“什么都不是。这不是一个选项。”
至于您可以将放入PostgreSQL的
查询
, psycopg2驱动程序文档可以使用。例如,如果您想创建一个使用数据库连接的引擎,其中需要SSL 您将使用类似query=
is how we specify anything that follows?
in the string version of the connection URL. (The values following?
have long been known as the "querystring" in a URL.)odbc_connect
is a special case for ODBC connections (e.g.,mssql+pyodbc://
). There is no equivalent for PostgreSQL because none of the supported drivers for thepostgresql
dialect use ODBC. So the answer tois "Nothing. That is not an option."
As for things you can put into
query
for PostgreSQL, any of the additionalkey=value
listed in the psycopg2 driver documentation could be used. For example, if you wanted to create an Engine that uses database connections where SSL is required you would use something like