SQLAlchemy:在Python中指定连接URL的正确方法是什么?

huangapple go评论85阅读模式
英文:

SQLAlchemy: proper way to specify connection URL in python

问题

使用SQLAlchemy(版本2.0.4)通过pymysql驱动程序从Python连接到远程MySQL服务器。

如果我以“简单”的方式声明连接URL,它可以正常工作:

import sqlalchemy as db
engine = db.create_engine("mysql+pymysql://dbuser:verysecret@myhost.com:9001/mydb")

但我更喜欢在教程中找到的这个版本:

import sqlalchemy as db
connect_url = db.engine.url.URL(
    "mysql+pymysql",
    username="dbuser",
    password="verysecret",
    host="myhost.com",
    port="9001",
    database="mydb",
)
engine = db.create_engine(connect_url)

但是在connect_url行会导致以下错误:

Traceback (most recent call last):
  File "/Users/bob/Documents/work/code/mysqltest/mysqltest/sqlalch.py", line 3, in <module>
    connect_url = db.engine.url.URL(
TypeError: URL.__new__() missing 1 required positional argument: 'query'

编辑:感谢@snakecharmer的回答,我随后尝试添加有效的查询参数如下:

import sqlalchemy as db

connect_url = db.engine.url.URL(
    "mysql+pymysql",
    username="root",
    password="ghj12WQ_AA1997",
    host="isagog.com",
    port=9001,
    database="athena",
    query=dict(charset="utf8mb4"),
)

engine = db.create_engine(connect_url)

这样可以正常工作。但仍然不确定为什么如果不提供查询参数就会出错。

英文:

Using SQLAlchemy (version 2.0.4)to connect to a remote MySQL server from python via the pymysql driver.

If I declare my connection URL in the "simple" way, it works:

import sqlalchemy as db
engine = db.create_engine(&quot;mysql+pymysql://dbuser:verysecret@myhost.com:9001/mydb&quot;)

but I'd prefer this other version I've found in a tutorial:

import sqlalchemy as db
connect_url = db.engine.url.URL(
    &quot;mysql+pymysql&quot;,
    username=&quot;dbuser&quot;,
    password=&quot;verysecret&quot;,
    host=&quot;myhost.com&quot;,
    port=&quot;9001&quot;,
    database=&quot;mydb&quot;,
)

engine = db.create_engine(connect_url)

but this fails on the connect_url line as follows:

Traceback (most recent call last):
  File &quot;/Users/bob/Documents/work/code/mysqltest/mysqltest/sqlalch.py&quot;, line 3, in &lt;module&gt;
    connect_url = db.engine.url.URL(
TypeError: URL.__new__() missing 1 required positional argument: &#39;query&#39;

EDIT: thanks to @snakecharmer's reply below I then tried to add a valid query parameter as follows:

import sqlalchemy as db

connect_url = db.engine.url.URL(
    &quot;mysql+pymysql&quot;,
    username=&quot;root&quot;,
    password=&quot;ghj12WQ_AA1997&quot;,
    host=&quot;isagog.com&quot;,
    port=9001,
    database=&quot;athena&quot;,
    query=dict(charset=&quot;utf8mb4&quot;),
)

engine = db.create_engine(connect_url)

And this does works well. But still not sure why I get an error if the query parameter is not provided.

答案1

得分: 1

如果不需要传递任何查询参数,请传递一个空字符串或字典作为查询部分:

&gt;&gt;&gt; url = sa.engine.URL('mysql+pymysql', 'root', 'root', 'localhost', 3306, 'test', {})
&gt;&gt;&gt; url
mysql+pymysql://root:***@localhost:3306/test

如果需要传递查询参数,请将它们作为字典传递:

&gt;&gt;&gt; import sqlalchemy as sa
&gt;&gt;&gt;
&gt;&gt;&gt; url = sa.engine.URL('mysql+pymysql', 'root', 'root', 'localhost', 3306, 'test', {'charset': 'utf8mb4'})
&gt;&gt;&gt; url
mysql+pymysql://root:***@localhost:3306/test?charset=utf8mb4
英文:

If you don't need to pass any query parameters, pass an empty string or dict for query:

&gt;&gt;&gt; url = sa.engine.URL(&#39;mysql+pymysql&#39;, &#39;root&#39;, &#39;root&#39;, &#39;localhost&#39;, 3306, &#39;test&#39;, {})
&gt;&gt;&gt; url
mysql+pymysql://root:***@localhost:3306/test

If you need to pass query arguments, pass them as a dictionary:

&gt;&gt;&gt; import sqlalchemy as sa
&gt;&gt;&gt;
&gt;&gt;&gt; url = sa.engine.URL(&#39;mysql+pymysql&#39;, &#39;root&#39;, &#39;root&#39;, &#39;localhost&#39;, 3306, &#39;test&#39;, {&#39;charset&#39;: &#39;utf8mb4&#39;})
&gt;&gt;&gt; url
mysql+pymysql://root:***@localhost:3306/test?charset=utf8mb4

huangapple
  • 本文由 发表于 2023年2月27日 16:14:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定