`sqlalchemy.engine.url.create`访问postgresql数据库的有效`Query'键是什么?

发布于 2025-02-09 15:23:34 字数 1518 浏览 2 评论 0原文

我有兴趣使用

这是参数:

  • 驱动器:数据库后端的名称。此名称将对应于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”和。

  1. (短期)我可以将查询更改为PostgreSQL和
  2. (从根本上)我可以找到详细的文档来理解查询>查询参数的可能输入?

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 name
  • password: database password. Is typically a string, but may also be an object that can be stringified with str().
  • 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 to create_engine(). See also URL.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.

  1. (short-term) What can I put into query to change to PostgreSQL and
  2. (more fundamentally) where can I find detailed documentation for understanding the possible inputs of the query parameter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶底世界 2025-02-16 15:23:34

query =是我们在连接URL的字符串版本中指定关注的内容。 (以下值早已被称为URL中的“ querystring”。)

odbc_connect是ODBC连接的一种特殊情况(例如,mssql+pyodbc ://)。 PostgreSQL没有等效的,因为PostgreSQL方言的支持驱动程序都没有使用ODBC。所以答案

我可以将什么放入查询更改为PostgreSQL

是“什么都不是。这不是一个选项。”

至于您可以将放入PostgreSQL的查询 psycopg2驱动程序文档可以使用。例如,如果您想创建一个使用数据库连接的引擎,其中需要SSL 您将使用类似

connection_url = URL.create(
    "postgresql+psycopg2",
    username="scott",
    password="tiger",
    host="192.168.0.199",
    database="test",
    query={"sslmode": "require"}
)
print(connection_url)
# postgresql+psycopg2://scott:[email protected]/test?sslmode=require

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 the postgresql dialect use ODBC. So the answer to

What can I put into query to change to PostgreSQL

is "Nothing. That is not an option."

As for things you can put into query for PostgreSQL, any of the additional key=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

connection_url = URL.create(
    "postgresql+psycopg2",
    username="scott",
    password="tiger",
    host="192.168.0.199",
    database="test",
    query={"sslmode": "require"}
)
print(connection_url)
# postgresql+psycopg2://scott:[email protected]/test?sslmode=require
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文